« How to use the Flex Dashboard Panel library | Main | How to enable communication between AIR and Skype through Merapi »

Importing REST web services in ActionScript with Flex Builder 4

Among the many Flex Builder 4 new features there is the possibility to import REST web services into ActionScript classes, in the same way that the previous builder allowed to import SOAP web services. It can import from XML as well as from JSON data. This new feature can obviously be used to import into ActionScript any kind of services that dynamically generate XML or JSON content.

For the following example I used the MAX preview version of Flex Builder 4, which has many interesting features and, while not yet complete, will become a very good IDE. The example will use a very simple JSP that returns some XML data, dynamically generated as a response to the client invocation. The JSP has contentType="text/xml;", accepts a numeric parameter length and returns a simple structure, with as many nodes as the length parameter. The service called with length=2 will return:

  <root>
    <content value="0"/>
    <content value="1"/>
  </root>

Now we can go to Flex Builder 4 and open the HTTP Service import dialog, right next to the Web Service one. The wizard will ask for the URL of the remote service and will be able to add the parameters accepted by the service. It is possible to define more operations, their parameters and request methods and also import as many HTTP services as we need. The next step will be the creation of the ActionScript classes, by selected the previously defined operations and configure the return type: the wizard will create all necessary classes and the methods to invoke the service, will hide from the developer the conversion between XML/JSON and ActionScript and provide with a transparent and object oriented set of classes ready to be used.

For the above XML the following classes have been created:
  Content.as - with a value property;
  XMLServiceTest - a class containing all the operations to be invoked on this service.
Both of these classes extend a base class, which contains the generated code, so we can safely modify the extending classes, as only the base ones would be overwritten in case of service changes.

Other interesting features of the new builder allow us to rapidly test the newly created services or even to enable paging and data management. By selecting a generated operation and the main application file we can have the builder generate a test datagrid and also all required events for it to work:

  <DataGrid id="DataGrid1" dataProvider="{result1.lastResult}">
    <columns>
      <DataGridColumn headerText="value" dataField="value"/>
    </columns>
  </DataGrid>

With a few clicks we have an application that exchanges strong typed data with a remote server, on top of XML/JSON.

By allowing a transparent conversion between XML/JSON data and ActionScript classes, this HTTP Service import feature makes the development easier for enterprise applications that use REST services or plain XML or JSON as a means of communication.

TrackBack

TrackBack URL for this entry:
http://blog.comtaste.com/mt-tb.cgi/76

Comments (9)

REST is huge and it's good to read about the ongoing efforts to support working with REST using Flex Builder. Thanks for the insight.

venkat:

Hi,

I have downloaded the flex sdk 4 and added to Flex 3 sdk. But could see the HttpService Wizard. Could you plese help me.

Nice informative post...thanks for sharing.

Hello venkat, the HttpService wizard is a tool integrated in Flex Builder 4 and is not related with the Flex 4 SDK. You will be able to use it when the builder will be released.

Stanley Guan:

When will Flex Builder 4 be released?

I have published my recent Actionscript library rest-arm. It can do all rest actions in an easy way. check it

http://code.google.com/p/rest-arm/

Great peace of work and the use of the web services in action script with the flex builder!
so good work!

Thanks for the nice information. I am sure, I will tweet this to my twitter account. This will help a lot of users.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on January 8, 2009 4:46 PM.

The previous post in this blog was How to use the Flex Dashboard Panel library.

The next post in this blog is How to enable communication between AIR and Skype through Merapi.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33