Web Services 基于Apache Axis2 生成客户端代码示例

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

1 Apache Axis2下载安装

1.1 认识Apache Axis2

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

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

  • 速度 - 轴 2 使用 它自己的对象模型和 StAX(XML 流 API)解析为 实现比早期版本的 Apache 更高的速度 轴。
  • 低内存足迹 - Axis2在设计时始终考虑到低内存足迹。
  • AXIOM - Axis2提供了自己的轻量级对象模型AXIOM,用于消息处理,该模型可扩展、高性能并且方便开发人员。
  • 热部署 - Axis2配备了在系统启动和运行时部署Web服务和处理程序的功能。换句话说,新的服务可以添加到系统中,而不必关闭服务器。只需将所需的Web服务存档放入存储库中的services目录中,部署模型就会自动部署该服务并使其可供使用。
  • 异步Web服务 - Axis2现在支持使用非阻塞客户机和传输的异步Web服务和异步Web服务调用。
  • MEP支持 - Axis2现在可以灵活地支持消息交换模式(MEP),并内置对WSDL 2.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 下载Apache Axis2

Apache Axis2 Download
发行版本下载图示

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 -uri http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?WSDL -p com.cloud.note.client -d adb -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.1 Apache Axis2 手工访问

package com.cloud.note.client;

import java.util.Iterator;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
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;

public class ClientMain {

	public static void main(String[] args) throws AxisFault {
		// 创建 ServiceClient 对象
		ServiceClient serviceClient = new ServiceClient();

		// 设置服务端点 URL
		EndpointReference targetEPR = new EndpointReference("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");
		Options options = serviceClient.getOptions();
		options.setTo(targetEPR);

		// 设置 Action 和操作名称
		options.setAction("http://WebXml.com.cn/getWeatherbyCityName");
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace ns = factory.createOMNamespace("http://WebXml.com.cn/", "ns");
		OMElement operation = factory.createOMElement("getWeatherbyCityName", ns);

		// 设置请求参数
		OMElement cityName = factory.createOMElement("theCityName", ns);
		cityName.setText("北京");
		operation.addChild(cityName);

		// 发送请求并接收响应
		OMElement response = serviceClient.sendReceive(operation);

		// 处理响应
		OMElement result = response.getFirstElement();
		String weather = result.getText();
		System.out.println("北京的天气: " + weather);

		// 遍历子元素
		OMElement child = result.getFirstElement();
		while (child != null) {
			String childName = child.getLocalName();
			String childValue = child.getText();
			System.out.println(childName + ": " + childValue);
			child = (OMElement) child.getNextOMSibling();
		}

		// 遍历属性
		Iterator<OMAttribute> attributes = result.getAllAttributes();
		while (attributes.hasNext()) {
			OMAttribute attribute = attributes.next();
			String attributeName = attribute.getLocalName();
			String attributeValue = attribute.getAttributeValue();
			System.out.println(attributeName + ": " + attributeValue);
		}

	}

	private void moreWays() throws AxisFault {
		// 1创建RPCServiceClient---此方法不适用天气查询
		RPCServiceClient serviceClient = new RPCServiceClient();
		// 配置服务地址(此处http://www.webxml.com.cn/WebServices/WeatherWebService.asmx为WSDL文件末尾的service location)
		Options options = serviceClient.getOptions();
		EndpointReference targetEPR = new EndpointReference("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");
		options.setTo(targetEPR);
		
		// 如果服务端由.NET编写完成,则需要设置Action,否则会出现  org.apache.axis2.AxisFault: 服务器未能识别 HTTP头 SOAPAction的值  异常
		//设置Action
		options.setAction("http://WebXml.com.cn/getWeatherbyCityName");
		// 设置要请求的方法名称和参数(此处http://WebXml.com.cn/为WSDL文件中的targetNamespace属性值)
		QName opAddEntry = new QName("http://WebXml.com.cn/", "getWeatherbyCityName");

		Object[] opAddEntryArgs = new Object[] { "杭州" };
		Class[] returnTypes = new Class[] { String.class };
		// 访问服务
		Object[] response = serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, returnTypes);

		// 处理响应
		System.out.println("杭州的天气:" + response[0]);
	}
}

3.2 通过生成的代码访问

package com.cloud.note.client;

import com.cloud.note.client.WeatherWebServiceStub.ArrayOfString;
import com.cloud.note.client.WeatherWebServiceStub.GetWeatherbyCityNameResponse;

public class ClientMain {

	public static void main(String[] args) throws Exception {
	
		WeatherWebServiceStub stub = new WeatherWebServiceStub();
		WeatherWebServiceStub.GetWeatherbyCityName getWeatherbyCityName = new WeatherWebServiceStub.GetWeatherbyCityName();
		getWeatherbyCityName.setTheCityName("杭州");
		GetWeatherbyCityNameResponse weatherbyCityName = stub.getWeatherbyCityName(getWeatherbyCityName);
		ArrayOfString getWeatherbyCityNameResult = weatherbyCityName.getGetWeatherbyCityNameResult();
		String[] strings = getWeatherbyCityNameResult.getString();
		for (int i = 0; i < strings.length; i++) {
			System.out.println(strings[i]);
		}
	}
}


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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基于 Apache Axis2,我们可以将上述类中的方法对外提供为 web 服务,并生成 WSDL 文件,同时也可以利用 Axis2 客户端代码调用该服务。 首先,我们需要在项目中引入 Axis2 的相关依赖包,并进行配置和部署。 1. 引入依赖包 首先,在项目的 pom.xml 文件中添加相关依赖: ```xml <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2</artifactId> <version>1.7.9</version> </dependency> ``` 2. 配置和部署 在项目的配置文件中,我们需要配置 Axis2 的相关信息,如 web.xml 文件中添加以下内容: ```xml <servlet> <servlet-name>AxisServlet</servlet-name> <display-name>Apache-Axis2</display-name> <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class> <init-param> <param-name>axis2.repository.path</param-name> <param-value>/WEB-INF/axis2.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> ``` 在项目的根目录下,创建文件夹 WEB-INF,再在 WEB-INF 目录下创建文件 axis2.xml,文件内容如下: ```xml <axisconfig name="AxisJava2.0"> <handler name="SimpleFileUploadHandler" class="org.apache.axis2.handlers.SimpleFileUploadHandler"/> <deployer extension=".aar" directory="axis2services"/> </axisconfig> ``` 然后,在项目中新建一个类来实现我们的 web 服务: ```java package com.example; public class ExampleService { public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 在该类中,我们提供了一个名为 sayHello 的方法,用于返回一个字符串。 接下来,我们可以使用 Axis2 的工具类来生成 WSDL 文件,即我们的服务描述文件。 ```java package com.example; import org.apache.axis2.description.AxisService; import org.apache.axis2.description.Parameter; import org.apache.axis2.description.WSDL2Constants; import org.apache.axis2.engine.AxisConfiguration; import org.apache.axis2.transport.http.AxisServlet; import org.apache.axis2.wsdl.WSDLConstants; import org.apache.axis2.wsdl.WSDLUtil; import org.apache.axis2.wsdl.WSDLConstants.WSDL20_2004_Constants; import org.apache.woden.WSDLFactory; import org.apache.woden.WSDLWriter; import javax.servlet.ServletContext; import javax.wsdl.WSDLException; import javax.wsdl.xml.WSDLLocator; import javax.wsdl.xml.WSDLWriter; import java.io.OutputStream; import java.util.Iterator; public class WsdlGenerator { private AxisServlet axisServlet; public WsdlGenerator(AxisServlet axisServlet) { this.axisServlet = axisServlet; } public void generateWsdlFile(OutputStream outputStream, String serviceName) throws WSDLException { AxisConfiguration axisConfiguration = axisServlet.getConfigurationContext().getAxisConfiguration(); AxisService axisService = axisConfiguration.getService(serviceName); WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter(); wsdlWriter.writeWSDL(new WSDL2Constants.WSDL(, new WSDLLocator() { @Override public String getBaseURI() { return null; } @Override public String getLatestImportURI() { return null; } @Override public java.io.Reader getBaseReader() { return null; } @Override public java.io.Reader getImportReader(String parentLocation, String importLocation) { return null; } @Override public javax.wsdl.xml.WSDLLocator getBaseLocator() { return null; } })); The generated WSDL file can then be returned to the caller by writing it to the OutputStream. 提供的参数 outputStream 是用于写入 WSDL 文件的输出流,serviceName 是指定的服务名称。 使用 Axis2 客户端调用我们的服务,可以使用以下代码: ```java package com.example.client; import org.apache.axis2.client.ServiceClient; import org.apache.axis2.transport.http.HTTPConstants; import org.apache.axis2.transport.http.HttpTransportProperties; public class ClientExample { public static void main(String[] args) throws Exception { ServiceClient serviceClient = new ServiceClient(); HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator(); auth.setUsername("username"); auth.setPassword("password"); serviceClient.getOptions().setProperty(HTTPConstants.AUTHENTICATE, auth); serviceClient.getOptions().setTo(targetEPR); serviceClient.sendReceive(operation); } } ``` 在该代码中,我们首先创建一个 ServiceClient 对象,然后设置用户名和密码,接着设置服务的目标地址,最后发送请求。 使用以上步骤,我们可以基于 Apache Axis2 将上述类的方法对外提供为 web 服务,生成 WSDL 文件,并可以使用 Axis2 客户端代码来调用该服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值