Exposing Java project as Web Service to Flash Builder 4 using Data Centric Development (DCD)

The definition of a Web Service as given by W3C is a software system designed to support interoperable machine-to-machine interaction over a network. It has an interface described in a machine-processable format (specifically WSDL). Other systems interact with the Web service in a manner prescribed by its description using SOAP-messages, typically conveyed using HTTP with an XML serialization in conjunction with other Web-related standard.

Web services are often used to provide an Application programming interface to clients systems which use the Server methods running on a remote system.

In this post I will talk about how to expose a java service as a web service. For accessing this service(or any other web service),  user requires a WSDL descriptor file. WSDL stands for Web Services Description Language. How to generate this WSDL will also be explained in this post.

I split this post in the following sections:
•    Creating database and Backend
•    Generating the WSDL
•    Connect to service using DCD in Flash Builder
•    Displaying data fetched by the service

Creating database and Backend

I will not go into the details of how to create the backend.
I am assuming user has a working Java project which can produce the data required by the client. In my opinion the implementation should be hidden to expose only an interface to the required methods. In any case, the user will have a class or an interface file containing declarations of needed methods. I will call this file BackendService.

Generating the WSDL

There are multilple tools available for generating the the WSDL. I have used Apache’s XFIRE. An upgrade of this tool with the name CFX is also available now. I would recommend using the older XFIRE for its simplicity. It can be downloaded from here .
User also needs to have a J2EE web server up and running. I recommend Tomcat but any other server will also do. On your server go to the directory where you would deploy your web applications(Web-apps) and create a directory with the name of your project. Here create another directory with the name WEB-INF. Open WEB-INF and now create the following subdirectories and files inside WEB-INF:

Classes

This directory should contain all the compiled Java classes with their package structure intact.  Also create a subdirectory with the name META-INF. Inside META-INF create another subdirectory with the name “xfire”. Create a new file “services.xml” in “xfire”.
Paste this xml code in Services.xml:
xml1
Change the name of the service to the name of your own service. Accordingly change the serviceClass tag to contain the path of the class from the “classes” directory. The namespace tag should contain your desired namespace.

LIB

In this directory copy all the libraries required by xfire. You can get the list of dependencies and their download links from here. Before downloading any check the libraries that you got from the Xfire download. Most of the dependencies will already be there.

web.xml

Now that you have created and populated the directories, create a new file in the WEB-INF directory with the name web.xml. In this file copy the following content:xml2

Now check out this URL:

http://localhost:8400/ProjectName/services/dataService?wsdl

In the above URL replace “ProjectName” with your actual project name. Replace dataService with the name of your dataService. Change the port if required and try accessing it from a browser.

You should now be able to view the generated WSDL . I will call this URL WSDL-URL. The next step is to access it from Flash builder.

Connect to service using DCD in Flash Builder

Open Flash builder and create a new project by going to file -> New -> Flex project. In the window that follows give an appropriate name to the project. I called it InvokeWSProject. Click Next->Finish to complete project completion.

1

Next step is to connect to the Java service. For this, Click on the Data/Services tab in the bottom frame of the screen.

2

In the screen that follows select Web service and click on next.

3

Enter WSDL-URL. Flash builder populates the Service details.

4

Click on next to see a screen like this. Here select the functions you want to import. All functions are selected by default.

5

When you click finish, you see all functions that you wished to import are now available to you in the Data/Services tab.

6

What remains now is to get the data fetched by the dataService and display them.

Displaying data fetched by the service

Go to Flash builder Design view. Drag and Drop a DataGrid from Flex component controls.

7

Your data will be shown in this grid. All that remains is to put the data in the grid. You already have the data methods in the Data/Services tab. Select the method whose output you want to see in the grid. Just drag and drop this method on the grid.

8

You see that the grid changes to the return value of the method. In my case it looked like this

9

Only thing remaining is to run the project. Press CTRL+F11 to run it.

10

Reviewing once …

User creates the database, populates it, writes a data service (dataService) to fetch the data. This completes the backend work. User exposes the service as a WSDL descriptor. User creates a flex project and links to the WSDL descriptor to get access to his functions on Client.

PS: User has not written a line of code on the Frontend J

QandA

User: Where can I get to know more about WSDL’s and web services?

Editor: http://en.wikipedia.org/wiki/Web_Services_Description_Language http://en.wikipedia.org/wiki/Web_service

User: I am using Eclipse IDE to code in Java. Is there some way I can generate my WSDL directly?

Editor: The Latest Eclipse build which can be downloaded from here has a feature of generating the WSDL. But the generated WSDL is just a prototype and needs work. Check out eclipse documentation to find out more. You can also download and install the axis plugin for eclipse. This tool directly converts Java to WSDL. Download the plugin from here. For installation instructions read this.

User: Flash Builder beta 2 was recently launched. Where can I download it?

Editor: To download Flash Builder 4 beta2  go here.

User: Any way I can know more about this product and its new features?

Editor:  Tim Buntel has a written an excellent article on what is new in this Beta. Here is the link: http://www.adobe.com/devnet/flex/articles/flashbuilder4_whatsnew.html

An aggregated list of articles on Flash Builder 4 was created by Sujit Reddy. Here is the link: http://sujitreddyg.wordpress.com/flash-builder-4/

Advertisements

2 Comments

  1. October 16, 2009 at 5:27 pm

    Hey, I found your blog while searching on Google. I have a blog on online stock trading, I’ll bookmark your site.

  2. Polprav said,

    October 20, 2009 at 11:45 am

    Hello from Russia!
    Can I quote a post in your blog with the link to you?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: