微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Axis2开发WebService客户端 的3种方式

第一RPC方式,不生成客户端代码

第二,document方式,不生成客户端代码

第三,用wsdl2java工具,生成客户端方式调用

  1. package samples.quickstart.client; 
  2.  
  3. import javax.xml.namespace.QName; 
  4. import org.apache.axiom.om.OMAbstractFactory; 
  5. import org.apache.axiom.om.OMElement; 
  6. import org.apache.axiom.om.OMFactory; 
  7. import org.apache.axiom.om.OMNamespace; 
  8. import org.apache.axis2.AxisFault; 
  9. import org.apache.axis2.addressing.EndpointReference; 
  10. import org.apache.axis2.client.Options; 
  11. import org.apache.axis2.client.ServiceClient; 
  12. import org.apache.axis2.rpc.client.RPCServiceClient; 
  13. import samples.quickstart.StockQuoteServiceStub; 
  14. import samples.quickstart.xsd.GetPrice; 
  15. import samples.quickstart.xsd.GetPriceResponse; 
  16.  
  17. public class StockQuoteClient { 
  18.  
  19.   /**
  20.    * 方法一:
  21.    * 应用rpc的方式调用 这种方式就等于远程调用
  22.    * 即通过url定位告诉远程服务器,告知方法名称,参数等, 调用远程服务,得到结果。
  23.    * 使用 org.apache.axis2.rpc.client.RPCServiceClient类调用WebService
  24.    *
  25.     【注】:
  26.    
  27.         如果被调用的WebService方法有返回值 应使用 invokeBlocking 方法方法有三个参数
  28.           第一个参数的类型是QName对象,表示要调用方法名;
  29.           第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
  30.             当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。
  31.           第三个参数表示WebService方法的 返回值类型的Class对象,参数类型为Class[]。
  32.        
  33.        
  34.         如果被调用的WebService方法没有返回值 应使用 invokeRobust 方法
  35.           该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。
  36.         在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,
  37.         也就是 <wsdl:deFinitions>元素的targetNamespace属性值。
  38.    *
  39.    */ 
  40.   public static void testRPcclient() { 
  41.     try
  42.       // axis1 服务端 
  43. // String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl"; 
  44.       // axis2 服务端 
  45.       String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService?wsdl"
  46.  
  47.       // 使用RPC方式调用WebService 
  48.       RPCServiceClient serviceClient = new RPCServiceClient(); 
  49.       // 指定调用WebService的URL 
  50.       EndpointReference targetEPR = new EndpointReference(url); 
  51.       Options options = serviceClient.getoptions(); 
  52.       //确定目标服务地址 
  53.       options.setTo(targetEPR); 
  54.       //确定调用方法 
  55.       options.setAction("urn:getPrice"); 
  56.  
  57.       /**
  58.        * 指定要调用的getPrice方法及WSDL文件的命名空间
  59.        * 如果 webservice 服务端由axis2编写
  60.        * 命名空间 不一致导致的问题
  61.        * org.apache.axis2.AxisFault: java.lang.RuntimeException: Unexpected subelement arg0
  62.        */ 
  63.       QName qname = new QName("http://quickstart.samples/xsd","getPrice"); 
  64.       // 指定getPrice方法的参数值 
  65.       Object[] parameters = new Object[] { "13" }; 
  66.        
  67.       // 指定getPrice方法返回值的数据类型的Class对象 
  68.       Class[] returnTypes = new Class[] { double.class }; 
  69.  
  70.       // 调用方法一 传递参数,调用服务,获取服务返回结果集 
  71.       OMElement element = serviceClient.invokeBlocking(qname,parameters); 
  72.       //值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。 
  73.       //我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用方法返回一个结果 
  74.       String result = element.getFirstElement().getText(); 
  75.       System.out.println(result); 
  76.  
  77.       // 调用方法二 getPrice方法输出方法的返回值 
  78.       Object[] response = serviceClient.invokeBlocking(qname,parameters,returnTypes); 
  79.       // String r = (String) response[0]; 
  80.       Double r = (Double) response[0]; 
  81.       System.out.println(r); 
  82.  
  83.     } catch (AxisFault e) { 
  84.       e.printstacktrace(); 
  85.     } 
  86.   } 
  87.  
  88.   /**
  89.    * 方法二: 应用document方式调用
  90.    * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合
  91.    */ 
  92.   public static void testDocument() { 
  93.     try
  94.       // String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService"; 
  95.       String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl"
  96.  
  97.       Options options = new Options(); 
  98.       // 指定调用WebService的URL 
  99.       EndpointReference targetEPR = new EndpointReference(url); 
  100.       options.setTo(targetEPR); 
  101.       // options.setAction("urn:getPrice"); 
  102.  
  103.       ServiceClient sender = new ServiceClient(); 
  104.       sender.setoptions(options); 
  105.        
  106.        
  107.       OMFactory fac = OMAbstractFactory.getoMFactory(); 
  108.       String tns = "http://quickstart.samples/"
  109.       // 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的 
  110.       OMNamespace omNs = fac.createOMNamespace(tns, ""); 
  111.  
  112.       OMElement method = fac.createOMElement("getPrice",omNs); 
  113.       OMElement symbol = fac.createOMElement("symbol",omNs); 
  114.       // symbol.setText("1"); 
  115.       symbol.addChild(fac.createOMText(symbol,"Axis2 Echo String ")); 
  116.       method.addChild(symbol); 
  117.       method.build(); 
  118.        
  119.       OMElement result = sender.sendReceive(method); 
  120.  
  121.       System.out.println(result); 
  122.  
  123.     } catch (AxisFault axisFault) { 
  124.       axisFault.printstacktrace(); 
  125.     } 
  126.   } 
  127.  
  128. /**
  129.   * 为SOAP Header构造验证信息,
  130.   * 如果你的服务端是没有验证的,那么你不用在Header中增加验证信息
  131.   *
  132.   * @param serviceClient
  133.   * @param tns 命名空间
  134.   * @param user
  135.   * @param passwrod
  136.   */ 
  137.   public void addValidation(ServiceClient serviceClient,String tns,String user,String passwrod) { 
  138.     OMFactory fac = OMAbstractFactory.getoMFactory(); 
  139.     OMNamespace omNs = fac.createOMNamespace(tns, "nsl"); 
  140.     OMElement header = fac.createOMElement("AuthenticationToken",omNs); 
  141.     OMElement ome_user = fac.createOMElement("Username",omNs); 
  142.     OMElement ome_pass = fac.createOMElement("Password",omNs); 
  143.      
  144.     ome_user.setText(user); 
  145.     ome_pass.setText(passwrod); 
  146.      
  147.     header.addChild(ome_user); 
  148.     header.addChild(ome_pass); 
  149.  
  150.     serviceClient.addHeader(header); 
  151.   } 
  152.  
  153.    
  154.   /**
  155.    * 方法三:利用axis2插件生成客户端方式调用
  156.    *
  157.    */ 
  158.   public static void testCodeClient() { 
  159.     try
  160.       String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService"
  161.       StockQuoteServiceStub stub = new StockQuoteServiceStub(url); 
  162.       GetPrice request = new GetPrice(); 
  163.       request.setSymbol("ABCD"); 
  164.       GetPriceResponse response = stub.getPrice(request); 
  165.       System.out.println(response.get_return()); 
  166.     } catch (org.apache.axis2.AxisFault e) { 
  167.       e.printstacktrace(); 
  168.     } catch (java.rmi.remoteexception e) { 
  169.       e.printstacktrace(); 
  170.     } 
  171.  
  172.   } 
  173.  
  174.   public static void main(String[] args) { 
  175.      StockQuoteClient.testRPcclient(); 
  176. // StockQuoteClient.testDocument(); 
  177.     // StockQuoteClient.testCodeClient(); 
  178.  
  179.   } 

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类

 

http://harveyzeng.iteye.com/blog/1849720

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐