Log In  
  Getting Started  
----------------------------------------

Resources

 

Creating a simple BlazeDS web service application in Flex Builder

This tutorial walks you through how to create a very simple web service application that uses the BlazeDS Proxy Service to access a SOAP-compliant web service. The BlazeDS Proxy Service proxies web service and HTTP service requests and eliminates the need for a Flash Player crossdomain.xml policy file on the server that hosts the web service. The Proxy Service also provides several other great advantages over using Flex to  directly access web services without it, as Flex engineer Peter Farland recently mentioned in his blog at http://blogs.adobe.com/pfarland/2007/12/blazeds_beta_1.html:

previous page

User rating?

  • Better REST functionality with support for HTTP 1.1 PUT, DELETE, HEAD, TRACE, OPTIONS in addition to GET and POST methods.
  • Support for third party URLs to be aliased so that they do not have to be hard coded or exposed in your application (and Proxy destinations can also be locked down so that only authorized users can tunnel though to a 3rd party destination).
  • Response data accessible even when the HTTP status code is not 200.
  • Log in to a third party endpoints that issue a Basic Authentication challenge without exposing the pop-up to the end user; you can build a custom Flex user interface as a login form and set the remote credentials for the proxy to use in pre-authenticating to a 3rd party endpoint.
  • Access to non-UTF-8-formatted text; the Proxy Service should be able to read in many more character set encodings. Because it is Java-based, the stream is converted into UCS-2 internally and then the Proxy Service always format this data back to Flash Player as UTF-8.

Server-side configuration

  1. In your copy of the blazeds web application, open the WEB-INF/flex/proxy-config.xml file in a text editor. If you are using BlazeDS with the integrated Tomcat server, the blazeds web application is located in install_dir/tomcat/webapps. The proxy-config.xml file contains configuration settings for the Proxy Service. If you open the services-config.xml file in the same directory, you will see that the proxy-config.xml file is included by reference in that file. The services-config.xml file is the top-level configuration file for BlazeDS. Generally, you reference configuration files for specific services, such as proxied web services and HTTP services, in this file . You also define system-wide settings such as messaging channels and security contraints in this file.
  2. In the proxy-config.xml file, just above the </service> element, add the following element and save the file:
        <destination id="quote">
            <properties>
                <wsdl>http://www.webservicex.net/stockquote.asmx?wsdl</wsdl>
                <soap>*</soap>
            </properties>
            <adapter ref="soap-proxy"/>
        </destination>
    

    This destination on the server is where requests from Flex clients are proxied. The <wsdl> element specifies the WSDL URL of a web service and the <soap> element specifies the web service endpoint URLs that can be proxied. In this case, the asterisk indicates that any endpoint URL is allowed. Note that there is a default channel called my-amf configured in the proxy-config.xml file. BlazeDS uses "channels" to send data back and forth between Flex clients and the server. The actual channel is defined in the services-config.xml file and it is only referenced in the proxy-config.xml file. This particular channel uses the Action Message Format (AMF). The <adapter> element references an adapter called soap-proxy, which is is designed to proxy web services.

  3. Start or restart your application server.

Client-side development

  1. In Flex Builder 3, create a new BlazeDS project as described here:
    http://learn.adobe.com/wiki/display/Flex/Using+Flex+Builder+with+your+J2EE+server
  2. Name the Flex Builder project stockquote.
  3. In the stockquote.mxml file that Flex Builder creates, you will create a Flex client application that requests and displays stock quotes from a web service.
  4. Just below the <mx:Application> element in the stockquote.mxml file, add an empty <mx:Script> element. You can do this by typing in the Flex Builder code view of the file, or by copying the following code:
    <mx:Script>
    	<![CDATA[
    
    	]]>
    </mx:Script>
    
  5. Just below the </mx:Script> element, add the following MXML code. This code creates WebService object that you use to call the service and handle results. In this application, we create a WebService object in MXML, but as with most Flex objects, you can optionally create them in ActionScript. Note that the useProxy property is set to true, the destination property is set to the quote destination we created. The result handler is set to a method we will create to handle the result of a web service operation.
    <mx:WebService id="srv" destination="quote"
     useProxy="true" showBusyCursor="true" result="onResult(event)"/>
    
  6. Just below the <mx:WebService> element, add the following MXML code. This code creates the user interface elements required to call the web service and display results. 
    <mx:TextArea width="200" height="50" id="ta"/>
    <mx:TextInput id="symbol" text="ADBE"/>
    <mx:Button label="Get Data" click="srv.GetQuote(symbol.text)"/>
    
  7. Enter the following ActionScript code within the CDATA section of the <mx:Script> element. This code handles the result data that the web service returns. I converts the result data contained in the ResultEvent from a String to an XML object called myXml. It then sets the text property of the ta TextArea control to the value of myXml.Stock.Last, which is a child element in the myXml object.
    import mx.rpc.events.ResultEvent;
    import mx.utils.ObjectUtil;
    
    public var myXml:XML;
    public function onResult(e:ResultEvent):void{
    	myXml = new XML(e.result);
    	this.ta.text = myXml.Stock.Last;
    }
    

    The MXML code is now complete.

  8. Make sure that your BlazeDS server is running.
  9. Compile and run the client application in Flex Builder by selecting Run > Run from the menu bar.
  10. Click the Get Data button to receive a stock quote.
previous page

Added by Mike Peterson , last edited by Randy Nielsen on Feb 21, 2008  (view change)
Labels: 
(None)

Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators