Axis2 WebService客戶端Axis2調用

第一RPC方式,不生成客戶端代碼java

第二,document方式,不生成客戶端代碼web

第三,用wsdl2java工具,生成客戶端方式調用apache

 

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類
測試

相關文章
相關標籤/搜索