当前位置:

Web Services Apache Axis2 生成客户端代码展示

访客 2024-01-29 462 0

webservice一种古老的技术,现在基本上已经弃用,但也有许多古董项目在用,整理并记录下自己的使用过程,避免更多的人踩坑。

1ApacheAxis2下载安装

1.1认识ApacheAxis2

ApacheAxis2™是一个WebServicesJSON/SOAP/WSDL引擎,是广泛使用的ApacheAxisSOAP堆栈。
ApacheAxis2™是一个开源的WebServices框架,支持SOAP和RESTfulWebServices。使用Maven来构建Axis2项目可以使项目更加规范,方便管理和部署。

Axis2具有许多新功能、增强功能和行业特性规范实现。提供的主要功能如下:遵循:

  • 速度-轴2使用它自己的对象模型和StAX(XML流API)解析为实现比早期版本的Apache更高的速度轴。
  • 低内存足迹-Axis2在设计时始终考虑到低内存足迹。
  • AXIOM-Axis2提供了自己的轻量级对象模型AXIOM,用于消息处理,该模型可扩展、高性能并且方便开发人员。
  • 热部署-Axis2配备了在系统启动和运行时部署Web服务和处理程序的功能。换句话说,新的服务可以添加到系统中,而不必关闭服务器。只需将所需的Web服务存档放入存储库中的services目录中,部署模型就会自动部署该服务并使其可供使用。
  • 异步Web服务-Axis2现在支持使用非阻塞客户机和传输的异步Web服务和异步Web服务调用。
  • MEP支持-Axis2现在可以灵活地支持消息交换模式(MEP),并内置对WSDL2.0中定义的基本MEP的支持。
  • 灵活性-Axis2体系结构为开发人员提供了完全的自由,可以将扩展插入到引擎中,以进行自定义报头处理、系统管理和任何您能想到的事情。
  • 稳定性-Axis2定义了一组已发布的接口,与Axis的其他部分相比,这些接口的变化相对缓慢。
  • 面向组件的部署-您可以轻松地定义handler的可重用网络,以实现应用程序的公共处理模式,或者将其分发给合作伙伴。
  • 传输框架-对于集成和使用传输,我们有一个干净简单的抽象(例如,在各种协议(如SMTP、FTP、面向消息的中间件等)上的SOAP的发送方和侦听器),并且引擎的核心是完全独立于传输的。
  • WSDL支持-Axis2支持Web服务描述语言(1.1和2.0版本),这允许您轻松构建存根以访问远程服务,还可以从Axis2自动导出已部署服务的机器可读描述。
  • JSON支持-Axis2支持使用JavaScript对象表示法以及GSON和Moshi创建Web服务,这允许您轻松构建基于POJO的接收和返回JSON的服务。
  • 组合和可扩展性-模块和阶段改进了对组合性和可扩展性的支持。模块支持可组合性,还可以以简单干净的方式支持新的WS-*规范。但是它们不能热部署,因为它们会改变系统的整体行为。

1.2下载ApacheAxis2

ApacheAxis2Download

2通过CMD生成客户端代码

我们以常用的天气预报Web服务中的getWeatherbyCityName1方法为示例,生成对应的客户端代码。
天气预报WSDL文档地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?WSDL
(1)将下载的axis2-1.8.2-bin.zip进行解压缩。
(2)通过cmd命令,进入到axis2-1.8.2\bin目录下。
(3)通过wsdl2java命令生成客户端代码,命令如下:

wsdl2java-urihttp://www.webxml.com.cn/WebServices/WeatherWebService.asmx?WSDL-pcom.cloud.note.client-dadb-s


(4)生成成功后,代码在bin/src文件夹内,如图所示

(5)将上述生成的Java文件拷贝到自己的项目中,同时引入pom.xml文件

<dependencies><dependency><groupId>dom4j</groupId><artifactId>dom4j</artifactId><version>1.6.1</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-kernel</artifactId><version>1.8.2</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-adb</artifactId><version>1.8.2</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-transport-local</artifactId><version>1.8.2</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-transport-http</artifactId><version>1.8.2</version></dependency></dependencies>

3实现服务端访问

3.1ApacheAxis2手工访问

packagecom.cloud.note.client;importjava.util.Iterator;importjavax.xml.namespace.QName;importorg.apache.axiom.om.OMAbstractFactory;importorg.apache.axiom.om.OMAttribute;importorg.apache.axiom.om.OMElement;importorg.apache.axiom.om.OMFactory;importorg.apache.axiom.om.OMNamespace;importorg.apache.axis2.AxisFault;importorg.apache.axis2.addressing.EndpointReference;importorg.apache.axis2.client.Options;importorg.apache.axis2.client.ServiceClient;importorg.apache.axis2.rpc.client.RPCServiceClient;publicclassClientMain{publicstaticvoidmain(String[]args)throwsAxisFault{//创建ServiceClient对象ServiceClientserviceClient=newServiceClient();//设置服务端点URLEndpointReferencetargetEPR=newEndpointReference("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");Optionsoptions=serviceClient.getOptions();options.setTo(targetEPR);//设置Action和操作名称options.setAction("http://WebXml.com.cn/getWeatherbyCityName");OMFactoryfactory=OMAbstractFactory.getOMFactory();OMNamespacens=factory.createOMNamespace("http://WebXml.com.cn/","ns");OMElementoperation=factory.createOMElement("getWeatherbyCityName",ns);//设置请求参数OMElementcityName=factory.createOMElement("theCityName",ns);cityName.setText("北京");operation.addChild(cityName);//发送请求并接收响应OMElementresponse=serviceClient.sendReceive(operation);//处理响应OMElementresult=response.getFirstElement();Stringweather=result.getText();System.out.println("北京的天气:"weather);//遍历子元素OMElementchild=result.getFirstElement();while(child!=null){StringchildName=child.getLocalName();StringchildValue=child.getText();System.out.println(childName":"childValue);child=(OMElement)child.getNextOMSibling();}//遍历属性Iterator<OMAttribute>attributes=result.getAllAttributes();while(attributes.hasNext()){OMAttributeattribute=attributes.next();StringattributeName=attribute.getLocalName();StringattributeValue=attribute.getAttributeValue();System.out.println(attributeName":"attributeValue);}}privatevoidmoreWays()throwsAxisFault{//1创建RPCServiceClient---此方法不适用天气查询RPCServiceClientserviceClient=newRPCServiceClient();//配置服务地址(此处http://www.webxml.com.cn/WebServices/WeatherWebService.asmx为WSDL文件末尾的servicelocation)Optionsoptions=serviceClient.getOptions();EndpointReferencetargetEPR=newEndpointReference("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");options.setTo(targetEPR);//如果服务端由.NET编写完成,则需要设置Action,否则会出现org.apache.axis2.AxisFault:服务器未能识别HTTP头SOAPAction的值异常//设置Actionoptions.setAction("http://WebXml.com.cn/getWeatherbyCityName");//设置要请求的方法名称和参数(此处http://WebXml.com.cn/为WSDL文件中的targetNamespace属性值)QNameopAddEntry=newQName("http://WebXml.com.cn/","getWeatherbyCityName");Object[]opAddEntryArgs=newObject[]{"杭州"};Class[]returnTypes=newClass[]{String.class};//访问服务Object[]response=serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,returnTypes);//处理响应System.out.println("杭州的天气:"response[0]);}}

3.2通过生成的代码访问

packagecom.cloud.note.client;importcom.cloud.note.client.WeatherWebServiceStub.ArrayOfString;importcom.cloud.note.client.WeatherWebServiceStub.GetWeatherbyCityNameResponse;publicclassClientMain{publicstaticvoidmain(String[]args)throwsException{WeatherWebServiceStubstub=newWeatherWebServiceStub();WeatherWebServiceStub.GetWeatherbyCityNamegetWeatherbyCityName=newWeatherWebServiceStub.GetWeatherbyCityName();getWeatherbyCityName.setTheCityName("杭州");GetWeatherbyCityNameResponseweatherbyCityName=stub.getWeatherbyCityName(getWeatherbyCityName);ArrayOfStringgetWeatherbyCityNameResult=weatherbyCityName.getGetWeatherbyCityNameResult();String[]strings=getWeatherbyCityNameResult.getString();for(inti=0;i<strings.length;i){System.out.println(strings[i]);}}}

根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数↩︎

发表评论

  • 评论列表
还没有人评论,快来抢沙发吧~