Spring + jax-ws + Flex Simple Implementation Example

Source: Internet
Author: User

Recently, consider how the Java service side interacts with flex clients more easily.

The first thought was AMF, followed by WebService. In contrast, WebService is more widely used. Therefore, first of all, record webservice ideas. The study of AMF will be documented in a later article.

To keep the code simple, use the JAX-WS implementation of Sun. Implement upload and download capabilities with Flex clients.


The first is the WebService interface declaration, note: the case of multiple parameters must be added Webparam annotations, otherwise only the first parameter can be recognized, the other parameters will be ignored.

@WebServicepublic interface Itestservice {@WebMethod public string upload (@WebParam string filename        , @WebParam byte[] bytes); @WebMethod public byte[] Download (String filename);

The following is an interface implementation code that implements two functions, processes the upload binary data and saves, reads the file and returns.

@WebService (servicename= "test") @Servicepublic  class testservice implements itestservice  {        @Override     public String  Upload (string filename, byte[] bytes)  {                        fileoutputstream  fos = null;        try {             file f = new file ("/tmp", filename);             system.out.println (F.getAbsolutePath ());             fos = new  FileOutputStream (f);             fos.write (bytes);        &nBSP;}   catch  (ioexception e)  {                         e.printstacktrace ();         } finally{             if (fos!=null) {                 try {                     fos.close ();                 } catch  (ioexception e)  {                                       E.printstacktrace ();                 }             }                     }        return  filename;    }     @Override     public  Byte[] download (String filename)  {         Bytearrayoutputstream bos = new bytearrayoutputstream ();         FileInputStream fis = null;         try {            fis = new  FileInputStream (New file ("/tmp", filename);             byte[] bytes =&Nbsp;new byte[1024];            int len  = -1;            while ((len =  fis.read (bytes))!=-1) {                 bos.write (Bytes, 0, len);             }                     } catch  (ioexception e)  {                          e.printstacktrace ();        } finally{             if (fis != null) {                 try {                     fis.close ();                 } catch  (ioexception e)  {                                           e.printstacktrace ();                 }            }         }        return  Bos.tobytearray ();     }}

Then, with spring integration, spring will automatically help us scan classes with WebService annotations and publish them as services. The IP here is set to 0.0.0.0 can be accessed on behalf of any IP on the server

<bean class= "Org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter" > <property name= "baseaddress "Value=" http://0.0.0.0:9080/ws/"/></bean>

After the service is successfully started, enter HTTP://LOCALHOST:9080/WS/TEST?WSDL in the browser to verify

The following is a call to the Flex client, which is also documented.

Public class testwsdl {        private var ws :webservice = new webservice;                         public function  TESTWSDL (url:string)          {      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;WS.LOADWSDL (URL);         }                         //  Prompt user to select Local file and upload         public  function upload ():void {             var fr:FileReference = new FileReference;             fr.aDdeventlistener (Event.complete, function ():void {                                           invoke ("Upload",  function (rs:string):void {                     trace (RS, " uploaded!");                 },  Fr.name,fr.data);            } );             fr.addeventlistener (Event.SELECT, function (): void {                           &nbSp;     fr.load ();                             } ) ;             fr.browse ();         }                 //  Download the file locally. This action will pop up the Download Box         public function download (): void {             var fr:FileReference =  new filereference;            var  filename:string =  "Test.txt";             invoke ("Download",  function (Rs:bytearray): Void {       &nbsP;        fr.save (Rs, filename);             },filename );         }                 //  Common ingress         public function invoke for client calls (MethodName:String,  callback:function, ...args):void {             ws.addeventlistener (Resultevent.result, function (re:resultevent):void {                 callback &&  callback (Re.result);                 ws.removeeventlistener (Resultevent.result, arguments.callee);             } );             Ws.addeventlistener (Faultevent.fault, function (fe:faultevent):void {                 trace (Fe.fault);                 ws.removeeventlistener ( Faultevent.fault, arguments.callee);             } );             var op: Abstractoperation = ws.getoperation (MethodName);             op.send.apply (Op, args);         }}

Finally, the calling code of the flex side

private var client:testwsdl = new testwsdl ("http://localhost:9080/ws/test?wsdl");        Private function upload (): void {client.upload ();} Private Function Download (): void {client.download ();}

At this point, the entire process is complete, of course, this is just the beginning, there are some more complex operations such as validation. The study is ripe and then mended.

Spring + jax-ws + Flex Simple Implementation Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.