使用Flex與java簡單訪問在Axis上即時發布型的WebService服務

來源:互聯網
上載者:User

        在異構系統中常常會使用到WebService,從其本身的含義上其更多展現的是一種標準。既然是標準,就像java世界的其他架構一樣,其產品並不是唯一的。WebService的實現架構套件括Axis,Axis2,XXFie,CXF等等。本文不進行深入探討WebService,僅舉一簡單例子,讓大家一方面對WebService有個簡單認識,另一方面在Flex和Java用戶端進行簡單訪問WebService服務,這也許是在項目中常做的事情。

        對於Axis來講,它支援兩種發布WebService服務的方式:一種是即時發布;另一種是定製發布。本例使用前者,只因其簡單,足可達到本文簡介目的,但同時要強調一點,它在正規的項目中是不適用用。原因是即時發布太簡單,簡單到有了服務的源碼,將其放到Tomcat等上即可使用,這點卻導致了其不夠靈活,一旦我們僅有jar包等卻沒有源碼時,則不可用了。言歸正傳,先發布一個WebService服務。

        在下載下來的axis-bin-1_4.zip中找到webapps\axis(,將axis整個檔案夾拷貝到Tomcat的webapps下,然後將下面的WebService服務類HelloFlexToWebService.jws,放到Tomcat\webapps\axis下。

            

public class HelloFlexToWebService{    public String getReturn(String userName)    {        return "you are accessing  WebService, " + userName;    }}

        運行tomcat,訪問 http://localhost:8080/axis/HelloFlexToWebService.jws?wsdl,我們會發現tomcat已經為我們自動將上面的服務解析為WSDL檔案了,簡單截取部分代碼如下所示,對比上面的服務類,相信我們會發先其對應關係。

     

<wsdl:message name="getReturnResponse">  <wsdl:part name="getReturnReturn" type="xsd:string" />   </wsdl:message>- <wsdl:message name="getReturnRequest">  <wsdl:part name="userName" type="xsd:string" />   </wsdl:message>- <wsdl:portType name="HelloFlexToWebService">- <wsdl:operation name="getReturn" parameterOrder="userName">  <wsdl:input message="impl:getReturnRequest" name="getReturnRequest" />   <wsdl:output message="impl:getReturnResponse" name="getReturnResponse" />   </wsdl:operation>  </wsdl:portType>

        對此WSDL檔案本文不多做解釋,總之它是就是我們對外發布的服務,用戶端通過Axis引擎訪問服務擷取相應資料。下面展示java用戶端訪問此服務的程式碼範例。

import java.rmi.RemoteException;import javax.xml.rpc.ServiceException;import javax.xml.namespace.QName;import org.apache.axis.client.Call;import org.apache.axis.client.Service;public class WSClient {public static void main(String[] args){String wsdlUrl = "http://localhost:8080/axis/HelloFlexToWebService.jws?wsdl";Service service = new Service();try {Call call = (Call)service.createCall();call.setTargetEndpointAddress(wsdlUrl);call.setOperationName(new QName(wsdlUrl,"getReturn"));try {String result = (String)call.invoke(new Object[]{"ChenWeiDong"});System.out.println(result);} catch (RemoteException e) {e.printStackTrace();}System.out.println();} catch (ServiceException e) {e.printStackTrace();}}}

        對比下面使用Flex訪問WebService服務的代碼如下所示:

<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"    xmlns:s="library://ns.adobe.com/flex/spark"    xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"><fx:Declarations><s:WebService id="webService" wsdl="http://localhost:8080/axis/HelloFlexToWebService.jws?wsdl"  showBusyCursor="true" result="onLoad(event)" fault="faultHandler(event)"/></fx:Declarations><mx:VBox><mx:TextInput id="cityname" text="ChenWeiDong"/><mx:Button label="CallWebService" click="callWebService(event)"/><mx:TextInput id="myWeatherReport" height="30" width="260" editable="false" /></mx:VBox><fx:Script><![CDATA[import mx.controls.Alert;import mx.rpc.events.FaultEvent;import mx.rpc.events.ResultEvent;/* *訪問webService服務*/protected function callWebService(event:MouseEvent):void{webService.getReturn(cityname.text);}/*  *訪問webService成功*/private function onLoad(event:ResultEvent):void{myWeatherReport.text = String(event.result);}/* *訪問webService失敗*/private function faultHandler(event:FaultEvent):void{Alert.show(event.fault.toString(),'WebService Error');}]]></fx:Script></s:Application>

        從Flex和java的用戶端代碼可以發現,訪問Webservice服務,都是先定位到服務,然後訪問服務對外提供的介面,擷取相應的資料。

        本文簡單描述了發布WebService服務和使用Java和Flex訪問所發布的服務。通過本文主要瞭解此WebService的原理性內容以及其實現架構的多樣化,為工作中使用它提供點基本思路,同時瞭解不同語言在訪問WebService上其實是大同小異的,原因是Webservice是一種標準,自然在使用上不會有過於偏離的地方。深刻掌握理解仍需要閱讀相應文獻。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.