Flex AS3 call a Java Webservice

Flex AS3 call a Java Webservice


Introduction

Webservice is a standard in soa architecture.
Flex is a leading tecnology for client side so let's see how actionscript 3(the flex programming language) can call a java(jax-ws) webservice.


Starting Point

The main step are:

  1. Make a WebProject with a Webservice
  2. Make a MXML in flex side and code the call

The webservice:

Two click with netbeans header and you can obtain this code:

package webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService()
public class testWS 
{
    @WebMethod(operationName = "sayHello")
    public String sayHello(@WebParam(name = "name")
    String name)
    {        
        return "hello " + name;
    }

}

The Action Script Side inside mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" click="useWebService()">

<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.soap.WebService;
public function useWebService():void
{
var ws:WebService = new WebService();
 
ws.addEventListener(ResultEvent.RESULT,onResult);
ws.addEventListener(FaultEvent.FAULT,onFault);  
 
ws.loadWSDL("http://yourserver/ws/testWSService?wsdl");
ws.sayHello("pippo");
}
private function onResult(event:ResultEvent):void
{
 trace("result " + event.result);
}
private function onFault(fault:FaultEvent):void
{
 trace("fault " + fault);
}
]]>
</mx:Script>
</mx:Application>

To see the result debug the flex application and see at console only after deploy the webservice.

Conclusion


This is the runtime version.

No comments: