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

2023-12-22 21:58

本文主要是介绍Axis2开发WebService 客户端的三种方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



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

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

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

Java代码 复制代码  收藏代码
  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.  
  37.         在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名, 
  38.         也就是 <wsdl:definitions>元素的targetNamespace属性值。 
  39.    * 
  40.    */  
  41.   public static void testRPCClient() {  
  42.     try {  
  43.       // axis1 服务端  
  44. // String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl";  
  45.       // axis2 服务端  
  46.       String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService?wsdl";  
  47.   
  48.       // 使用RPC方式调用WebService  
  49.       RPCServiceClient serviceClient = new RPCServiceClient();  
  50.       // 指定调用WebService的URL  
  51.       EndpointReference targetEPR = new EndpointReference(url);  
  52.       Options options = serviceClient.getOptions();  
  53.       //确定目标服务地址  
  54.       options.setTo(targetEPR);  
  55.       //确定调用方法  
  56.       options.setAction("urn:getPrice");  
  57.   
  58.       /** 
  59.        * 指定要调用的getPrice方法及WSDL文件的命名空间 
  60.        * 如果 webservice 服务端由axis2编写 
  61.        * 命名空间 不一致导致的问题 
  62.        * org.apache.axis2.AxisFault: java.lang.RuntimeException: Unexpected subelement arg0 
  63.        */  
  64.       QName qname = new QName("http://quickstart.samples/xsd""getPrice");  
  65.       // 指定getPrice方法的参数值  
  66.       Object[] parameters = new Object[] { "13" };  
  67.         
  68.       // 指定getPrice方法返回值的数据类型的Class对象  
  69.       Class[] returnTypes = new Class[] { double.class };  
  70.   
  71.       // 调用方法一 传递参数,调用服务,获取服务返回结果集  
  72.       OMElement element = serviceClient.invokeBlocking(qname, parameters);  
  73.       //值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。  
  74.       //我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果  
  75.       String result = element.getFirstElement().getText();  
  76.       System.out.println(result);  
  77.   
  78.       // 调用方法二 getPrice方法并输出该方法的返回值  
  79.       Object[] response = serviceClient.invokeBlocking(qname, parameters, returnTypes);  
  80.       // String r = (String) response[0];  
  81.       Double r = (Double) response[0];  
  82.       System.out.println(r);  
  83.   
  84.     } catch (AxisFault e) {  
  85.       e.printStackTrace();  
  86.     }  
  87.   }  
  88.   
  89.   /** 
  90.    * 方法二: 应用document方式调用 
  91.    * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合 
  92.    */  
  93.   public static void testDocument() {  
  94.     try {  
  95.       // String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService";  
  96.       String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl";  
  97.   
  98.       Options options = new Options();  
  99.       // 指定调用WebService的URL  
  100.       EndpointReference targetEPR = new EndpointReference(url);  
  101.       options.setTo(targetEPR);  
  102.       // options.setAction("urn:getPrice");  
  103.   
  104.       ServiceClient sender = new ServiceClient();  
  105.       sender.setOptions(options);  
  106.         
  107.         
  108.       OMFactory fac = OMAbstractFactory.getOMFactory();  
  109.       String tns = "http://quickstart.samples/";  
  110.       // 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的  
  111.       OMNamespace omNs = fac.createOMNamespace(tns, "");  
  112.   
  113.       OMElement method = fac.createOMElement("getPrice", omNs);  
  114.       OMElement symbol = fac.createOMElement("symbol", omNs);  
  115.       // symbol.setText("1");  
  116.       symbol.addChild(fac.createOMText(symbol, "Axis2 Echo String "));  
  117.       method.addChild(symbol);  
  118.       method.build();  
  119.         
  120.       OMElement result = sender.sendReceive(method);  
  121.   
  122.       System.out.println(result);  
  123.   
  124.     } catch (AxisFault axisFault) {  
  125.       axisFault.printStackTrace();  
  126.     }  
  127.   }  
  128.   
  129.  /** 
  130.   * 为SOAP Header构造验证信息, 
  131.   * 如果你的服务端是没有验证的,那么你不用在Header中增加验证信息 
  132.   * 
  133.   * @param serviceClient 
  134.   * @param tns 命名空间 
  135.   * @param user 
  136.   * @param passwrod 
  137.   */  
  138.   public void addValidation(ServiceClient serviceClient, String tns , String user, String passwrod) {  
  139.     OMFactory fac = OMAbstractFactory.getOMFactory();  
  140.     OMNamespace omNs = fac.createOMNamespace(tns, "nsl");  
  141.     OMElement header = fac.createOMElement("AuthenticationToken", omNs);  
  142.     OMElement ome_user = fac.createOMElement("Username", omNs);  
  143.     OMElement ome_pass = fac.createOMElement("Password", omNs);  
  144.       
  145.     ome_user.setText(user);  
  146.     ome_pass.setText(passwrod);  
  147.       
  148.     header.addChild(ome_user);  
  149.     header.addChild(ome_pass);  
  150.   
  151.     serviceClient.addHeader(header);  
  152.   }  
  153.   
  154.     
  155.   /** 
  156.    * 方法三:利用axis2插件生成客户端方式调用 
  157.    * 
  158.    */  
  159.   public static void testCodeClient() {  
  160.     try {  
  161.       String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService";  
  162.       StockQuoteServiceStub stub = new StockQuoteServiceStub(url);  
  163.       GetPrice request = new GetPrice();  
  164.       request.setSymbol("ABCD");  
  165.       GetPriceResponse response = stub.getPrice(request);  
  166.       System.out.println(response.get_return());  
  167.     } catch (org.apache.axis2.AxisFault e) {  
  168.       e.printStackTrace();  
  169.     } catch (java.rmi.RemoteException e) {  
  170.       e.printStackTrace();  
  171.     }  
  172.   
  173.   }  
  174.   
  175.   public static void main(String[] args) {  
  176.      StockQuoteClient.testRPCClient();  
  177. // StockQuoteClient.testDocument();  
  178.     // StockQuoteClient.testCodeClient();  
  179.   
  180.   }  
  181. }  
    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方式调用WebServiceRPCServiceClient serviceClient = new RPCServiceClient();// 指定调用WebService的URLEndpointReference 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的URLEndpointReference 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 客户端的三种方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/525616

相关文章

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Spring中配置ContextLoaderListener方式

《Spring中配置ContextLoaderListener方式》:本文主要介绍Spring中配置ContextLoaderListener方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录Spring中配置ContextLoaderLishttp://www.chinasem.cntene

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA