axis2調用webService幾種方式

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   os   使用   io   檔案   

主要有三種方式:

第一RPC方式,不產生用戶端代碼

第二,document方式,不產生用戶端代碼

第三,用wsdl2java工具,產生用戶端方式調用

java代碼:

    package samples.quickstart.client;    import javax.xml.namespace.QName;    import org.apache.axiom.om.OMAbstractFactory;    import org.apache.axiom.om.OMElement;    import org.apache.axiom.om.OMFactory;    import org.apache.axiom.om.OMNamespace;    import org.apache.axis2.AxisFault;    import org.apache.axis2.addressing.EndpointReference;    import org.apache.axis2.client.Options;    import org.apache.axis2.client.ServiceClient;    import org.apache.axis2.rpc.client.RPCServiceClient;    import samples.quickstart.StockQuoteServiceStub;    import samples.quickstart.xsd.GetPrice;    import samples.quickstart.xsd.GetPriceResponse;    public class StockQuoteClient {      /**       * 方法一:       * 應用rpc的方式調用 這種方式就等於遠程調用,       * 即通過url定位告訴遠程伺服器,告知方法名稱,參數等, 調用遠程服務,得到結果。       * 使用 org.apache.axis2.rpc.client.RPCServiceClient類調用WebService       *        【注】:                    如果被調用的WebService方法有傳回值 應使用 invokeBlocking 方法 該方法有三個參數              第一個參數的類型是QName對象,表示要調用的方法名;              第二個參數表示要調用的WebService方法的參數值,參數類型為Object[];                當方法沒有參數時,invokeBlocking方法的第二個參數值不能是null,而要使用new Object[]{}。              第三個參數表示WebService方法的 傳回值類型的Class對象,參數類型為Class[]。                                    如果被調用的WebService方法沒有傳回值 應使用 invokeRobust 方法              該方法只有兩個參數,它們的含義與invokeBlocking方法的前兩個參數的含義相同。            在建立QName對象時,QName類的構造方法的第一個參數表示WSDL檔案的命名空間名,            也就是 <wsdl:definitions>元素的targetNamespace屬性值。       *       */      public static void testRPCClient() {        try {          // axis1 服務端    // String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl";          // axis2 服務端          String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService?wsdl";          // 使用RPC方式調用WebService          RPCServiceClient serviceClient = new RPCServiceClient();          // 指定調用WebService的URL          EndpointReference targetEPR = new EndpointReference(url);          Options options = serviceClient.getOptions();          //確定目標服務地址          options.setTo(targetEPR);          //確定調用方法          options.setAction("urn:getPrice");          /**           * 指定要調用的getPrice方法及WSDL檔案的命名空間           * 如果 webservice 服務端由axis2編寫           * 命名空間 不一致導致的問題           * org.apache.axis2.AxisFault: java.lang.RuntimeException: Unexpected subelement arg0           */          QName qname = new QName("http://quickstart.samples/xsd", "getPrice");          // 指定getPrice方法的參數值          Object[] parameters = new Object[] { "13" };                    // 指定getPrice方法傳回值的資料類型的Class對象          Class[] returnTypes = new Class[] { double.class };          // 調用方法一 傳遞參數,調用服務,擷取服務返回結果集          OMElement element = serviceClient.invokeBlocking(qname, parameters);          //值得注意的是,返回結果就是一段由OMElement對象封裝的xml字串。          //我們可以對之靈活應用,下面我取第一個元素值,並列印之。因為調用的方法返回一個結果          String result = element.getFirstElement().getText();          System.out.println(result);          // 調用方法二 getPrice方法並輸出該方法的傳回值          Object[] response = serviceClient.invokeBlocking(qname, parameters, returnTypes);          // String r = (String) response[0];          Double r = (Double) response[0];          System.out.println(r);        } catch (AxisFault e) {          e.printStackTrace();        }      }      /**       * 方法二: 應用document方式調用       * 用ducument方式應用現對繁瑣而靈活。現在用的比較多。因為真正擺脫了我們不想要的耦合       */      public static void testDocument() {        try {          // String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService";          String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl";          Options options = new Options();          // 指定調用WebService的URL          EndpointReference targetEPR = new EndpointReference(url);          options.setTo(targetEPR);          // options.setAction("urn:getPrice");          ServiceClient sender = new ServiceClient();          sender.setOptions(options);                              OMFactory fac = OMAbstractFactory.getOMFactory();          String tns = "http://quickstart.samples/";          // 命名空間,有時命名空間不增加沒事,不過最好加上,因為有時有事,你懂的          OMNamespace omNs = fac.createOMNamespace(tns, "");          OMElement method = fac.createOMElement("getPrice", omNs);          OMElement symbol = fac.createOMElement("symbol", omNs);          // symbol.setText("1");          symbol.addChild(fac.createOMText(symbol, "Axis2 Echo String "));          method.addChild(symbol);          method.build();                    OMElement result = sender.sendReceive(method);          System.out.println(result);        } catch (AxisFault axisFault) {          axisFault.printStackTrace();        }      }     /**      * 為SOAP Header構造驗證資訊,      * 如果你的服務端是沒有驗證的,那麼你不用在Header中增加驗證資訊      *      * @param serviceClient      * @param tns 命名空間      * @param user      * @param passwrod      */      public void addValidation(ServiceClient serviceClient, String tns , String user, String passwrod) {        OMFactory fac = OMAbstractFactory.getOMFactory();        OMNamespace omNs = fac.createOMNamespace(tns, "nsl");        OMElement header = fac.createOMElement("AuthenticationToken", omNs);        OMElement ome_user = fac.createOMElement("Username", omNs);        OMElement ome_pass = fac.createOMElement("Password", omNs);                ome_user.setText(user);        ome_pass.setText(passwrod);                header.addChild(ome_user);        header.addChild(ome_pass);        serviceClient.addHeader(header);      }            /**       * 方法三:利用axis2外掛程式產生用戶端方式調用       *       */      public static void testCodeClient() {        try {          String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService";          StockQuoteServiceStub stub = new StockQuoteServiceStub(url);          GetPrice request = new GetPrice();          request.setSymbol("ABCD");          GetPriceResponse response = stub.getPrice(request);          System.out.println(response.get_return());        } catch (org.apache.axis2.AxisFault e) {          e.printStackTrace();        } catch (java.rmi.RemoteException e) {          e.printStackTrace();        }      }      public static void main(String[] args) {         StockQuoteClient.testRPCClient();    // StockQuoteClient.testDocument();        // StockQuoteClient.testCodeClient();      }    }

 

wsdl2java 用於根據WSDL產生相應的服務端和用戶端代碼的產生工具。
命令列格式為:WSDL2Java [options] -uri <url or path> : A url or path to a WSDL

例如:

wsdl2java -uri http://localhost:8080/cxfService_0617/services/Hellows?wsdl -s -o build\client

 

其中常用的options具體如下:
-o <path> : 指定產生代碼的輸出路徑
-a : 產生非同步模式的代碼
-s : 產生同步模式的代碼
-p <pkg> : 指定代碼的package名稱
-l <languange> : 使用的語言(Java/C) 預設是java
-t : 為代碼產生測試案例
-ss : 產生服務端代碼 預設不產生
-sd : 產生服務描述檔案 services.xml,僅與-ss一同使用
-d <databinding> : 指定databingding,例如,adb,xmlbean,jibx,jaxme and jaxbri
-g : 產生服務端和用戶端的代碼
-pn <port_name> : 當WSDL中有多個port時,指定其中一個port
-sn <serv_name> : 選擇WSDL中的一個service
-u : 展開data-binding的類
-r <path> : 為代碼產生指定一個repository
-ssi : 為服務端實現代碼產生介面類
-S : 為產生的源碼指定儲存路徑
-R : 為產生的resources指定儲存路徑
–noBuildXML : 輸出中不產生build.xml檔案
–noWSDL : 在resources目錄中不產生WSDL檔案
–noMessageReceiver : 不產生MessageReceiver類

 

 

axis2調用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.