axis2 客户端调用三种方式

1、wsdl文件:

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://jh.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://jh.com">  
  3.   <wsdl:documentation>TestWeb</wsdl:documentation>   
  4. <wsdl:types>  
  5. <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://jh.com">  
  6. <xs:element name="getName">  
  7. <xs:complexType>  
  8. <xs:sequence>  
  9.   <xs:element minOccurs="0" name="name" nillable="true" type="xs:string" />   
  10.   </xs:sequence>  
  11.   </xs:complexType>  
  12.   </xs:element>  
  13. <xs:element name="getNameResponse">  
  14. <xs:complexType>  
  15. <xs:sequence>  
  16.   <xs:element minOccurs="0" name="return" nillable="true" type="xs:string" />   
  17.   </xs:sequence>  
  18.   </xs:complexType>  
  19.   </xs:element>  
  20.   </xs:schema>  
  21.   </wsdl:types>  
  22. <wsdl:message name="getNameRequest">  
  23.   <wsdl:part name="parameters" element="ns:getName" />   
  24.   </wsdl:message>  
  25. <wsdl:message name="getNameResponse">  
  26.   <wsdl:part name="parameters" element="ns:getNameResponse" />   
  27.   </wsdl:message>  
  28. <wsdl:portType name="TestWebPortType">  
  29. <wsdl:operation name="getName">  
  30.   <wsdl:input message="ns:getNameRequest" wsaw:Action="urn:getName" />   
  31.   <wsdl:output message="ns:getNameResponse" wsaw:Action="urn:getNameResponse" />   
  32.   </wsdl:operation>  
  33.   </wsdl:portType>  
  34. <wsdl:binding name="TestWebSoap11Binding" type="ns:TestWebPortType">  
  35.   <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />   
  36. <wsdl:operation name="getName">  
  37.   <soap:operation soapAction="urn:getName" style="document" />   
  38. <wsdl:input>  
  39.   <soap:body use="literal" />   
  40.   </wsdl:input>  
  41. <wsdl:output>  
  42.   <soap:body use="literal" />   
  43.   </wsdl:output>  
  44.   </wsdl:operation>  
  45.   </wsdl:binding>  
  46. <wsdl:binding name="TestWebSoap12Binding" type="ns:TestWebPortType">  
  47.   <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />   
  48. <wsdl:operation name="getName">  
  49.   <soap12:operation soapAction="urn:getName" style="document" />   
  50. <wsdl:input>  
  51.   <soap12:body use="literal" />   
  52.   </wsdl:input>  
  53. <wsdl:output>  
  54.   <soap12:body use="literal" />   
  55.   </wsdl:output>  
  56.   </wsdl:operation>  
  57.   </wsdl:binding>  
  58. <wsdl:binding name="TestWebHttpBinding" type="ns:TestWebPortType">  
  59.   <http:binding verb="POST" />   
  60. <wsdl:operation name="getName">  
  61.   <http:operation location="getName" />   
  62. <wsdl:input>  
  63.   <mime:content type="application/xml" part="parameters" />   
  64.   </wsdl:input>  
  65. <wsdl:output>  
  66.   <mime:content type="application/xml" part="parameters" />   
  67.   </wsdl:output>  
  68.   </wsdl:operation>  
  69.   </wsdl:binding>  
  70. <wsdl:service name="TestWeb">  
  71. <wsdl:port name="TestWebHttpSoap11Endpoint" binding="ns:TestWebSoap11Binding">  
  72.   <soap:address location="http://localhost:9090/axis2/services/TestWeb.TestWebHttpSoap11Endpoint/" />   
  73.   </wsdl:port>  
  74. <wsdl:port name="TestWebHttpSoap12Endpoint" binding="ns:TestWebSoap12Binding">  
  75.   <soap12:address location="http://localhost:9090/axis2/services/TestWeb.TestWebHttpSoap12Endpoint/" />   
  76.   </wsdl:port>  
  77. <wsdl:port name="TestWebHttpEndpoint" binding="ns:TestWebHttpBinding">  
  78.   <http:address location="http://localhost:9090/axis2/services/TestWeb.TestWebHttpEndpoint/" />   
  79.   </wsdl:port>  
  80.   </wsdl:service>  
  81.   </wsdl:definitions>  

 

2、java调用方式:

 

Java代码   收藏代码
  1. package com.jh;  
  2.   
  3. import java.rmi.RemoteException;  
  4.   
  5. import javax.xml.namespace.QName;  
  6.   
  7. import org.apache.axiom.om.OMAbstractFactory;  
  8. import org.apache.axiom.om.OMElement;  
  9. import org.apache.axiom.om.OMFactory;  
  10. import org.apache.axiom.om.OMNamespace;  
  11. import org.apache.axis2.AxisFault;  
  12. import org.apache.axis2.addressing.EndpointReference;  
  13. import org.apache.axis2.client.Options;  
  14. import org.apache.axis2.client.ServiceClient;  
  15. import org.apache.axis2.rpc.client.RPCServiceClient;  
  16.   
  17. public class Test {  
  18.   
  19.     public static void main(String[] args) throws RemoteException {  
  20.         Test test = new Test();  
  21.   
  22.         System.out.println("1   " + test.method1());  
  23.         System.out.println("2    " + test.method2());  
  24.         System.out.println("3    " + test.method3());  
  25.     }  
  26.   
  27.     /** 
  28.      * 方法一:通过 wsdl2java反向生成的类 调用 
  29.      * @return 
  30.      * @throws RemoteException 
  31.      */  
  32.     public String method1() throws RemoteException {  
  33.   
  34.         TestWeb web = new TestWebStub();  
  35.   
  36.         GetName getName = new GetName();  
  37.         getName.setName("admin ... ");  
  38.         GetNameResponse res = web.getName(getName);  
  39.   
  40.         System.out.println(res.get_return());  
  41.   
  42.         return res.get_return();  
  43.     }  
  44.   
  45.     /**  
  46.      * 方法二:  
  47.      * 应用rpc的方式调用 这种方式就等于远程调用,  
  48.      * 即通过url定位告诉远程服务器,告知方法名称,参数等, 调用远程服务,得到结果。  
  49.      * 使用 org.apache.axis2.rpc.client.RPCServiceClient类调用WebService  
  50.      *  
  51.         【注】:  
  52.           
  53.             如果被调用的WebService方法有返回值 应使用 invokeBlocking 方法 该方法有三个参数  
  54.               第一个参数的类型是QName对象,表示要调用的方法名;  
  55.               第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];  
  56.                 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。  
  57.               第三个参数表示WebService方法的 返回值类型的Class对象,参数类型为Class[]。  
  58.               
  59.               
  60.             如果被调用的WebService方法没有返回值 应使用 invokeRobust 方法  
  61.               该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。  
  62.       
  63.             在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,  
  64.             也就是 <wsdl:definitions>元素的targetNamespace属性值。  
  65.      *  
  66.      */  
  67.     public String method2() throws AxisFault {  
  68.         String url = "http://localhost:9090/axis2/services/TestWeb?wsdl";  
  69.   
  70.         // 使用RPC方式调用WebService    
  71.         RPCServiceClient serviceClient = new RPCServiceClient();  
  72.         // 指定调用WebService的URL    
  73.         EndpointReference targetEPR = new EndpointReference(url);  
  74.         Options options = serviceClient.getOptions();  
  75.         //确定目标服务地址    
  76.         options.setTo(targetEPR);  
  77.         //确定调用方法    
  78.         options.setAction("urn:getName");  
  79.   
  80.         /**  
  81.          * 指定要调用的getPrice方法及WSDL文件的命名空间  
  82.          * 如果 webservice 服务端由axis2编写  
  83.          * 命名空间 不一致导致的问题  
  84.          * org.apache.axis2.AxisFault: java.lang.RuntimeException: Unexpected subelement arg0  
  85.          */  
  86.         QName qname = new QName("http://jh.com""getName");  
  87.         // 指定getPrice方法的参数值    
  88.         Object[] parameters = new Object[] { "admin... 您终于进来了" };  
  89.   
  90.         // 指定getPrice方法返回值的数据类型的Class对象    
  91.         Class[] returnTypes = new Class[] { String.class };  
  92.   
  93.         // 调用方法一 传递参数,调用服务,获取服务返回结果集    
  94.         OMElement element = serviceClient.invokeBlocking(qname, parameters);  
  95.         //值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。    
  96.         //我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果    
  97.         String result = element.getFirstElement().getText();  
  98.         System.out.println(result);  
  99.         return result;  
  100.     }  
  101.   
  102.     /**  
  103.      * 方法三: 应用document方式调用  
  104.      * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合  
  105.      */  
  106.     public String method3() {  
  107.   
  108.         OMElement result = null;  
  109.         try {  
  110.             // String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService";    
  111.             String url = "http://localhost:9090/axis2/services/TestWeb?wsdl";  
  112.   
  113.             Options options = new Options();  
  114.             // 指定调用WebService的URL    
  115.             EndpointReference targetEPR = new EndpointReference(url);  
  116.             options.setTo(targetEPR);  
  117.             // options.setAction("urn:getPrice");    
  118.   
  119.             ServiceClient sender = new ServiceClient();  
  120.             sender.setOptions(options);  
  121.   
  122.             OMFactory fac = OMAbstractFactory.getOMFactory();  
  123.             String tns = "http://jh.com";  
  124.             // 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的    
  125.             OMNamespace omNs = fac.createOMNamespace(tns, "");  
  126.   
  127.             OMElement method = fac.createOMElement("getName", omNs);  
  128.             OMElement symbol = fac.createOMElement("name", omNs);  
  129.             // symbol.setText("1");    
  130.             symbol.addChild(fac.createOMText(symbol, "Axis2 Echo String "));  
  131.             method.addChild(symbol);  
  132.             method.build();  
  133.   
  134.             result = sender.sendReceive(method);  
  135.   
  136.             System.out.println("*************** " + result);  
  137.             //<ns:getNameResponse xmlns:ns="http://jh.com"><ns:return>欢迎您。 Axis2 Echo String </ns:return></ns:getNameResponse>  
  138.   
  139.         } catch (AxisFault axisFault) {  
  140.             axisFault.printStackTrace();  
  141.         }  
  142.         return result + "";  
  143.     }  
  144. }  

 

 

3、输出结果:

Html代码   收藏代码
  1. 欢迎您。 admin ...   
  2. 1   欢迎您。 admin ...   
  3. 欢迎您。 admin... 您终于进来了  
  4. 2    欢迎您。 admin... 您终于进来了  
  5. *************** <ns:getNameResponse xmlns:ns="http://jh.com"><ns:return>欢迎您。 Axis2 Echo String </ns:return></ns:getNameResponse>  
  6. 3    <ns:getNameResponse xmlns:ns="http://jh.com"><ns:return>欢迎您。 Axis2 Echo String </ns:return></ns:getNameResponse>  

  

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值