自己根据网上找的一些资料,写的一个调用axis2接口的客户端代码
首先依赖的包
<!-- axis2 -->
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.4.1</version>
</dependency>
/**
*
*/
package com.common.utils;
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;
/**
* axis2客户端工具类
* @author luolin
*
* @version $id:Axis2ClientUtils.java,v 0.1 2015年11月26日 下午3:10:50 luolin Exp $
*/
public class Axis2ClientUtils {
/**
* 调用axis2接口
* @param url wsdl地址
* @param targetNamespace 命名空间
* @param method 目标方法
* @param args 参数集合,必须和目标接口的参数顺讯相同
* @return 调用结果
* @throws AxisFault
*/
public static String callAxis2Server(String url, String targetNamespace, String method, Object[] args)
throws AxisFault {
Options options = new Options();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(url);
options.setTo(targetEPR);
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMFactory fac = OMAbstractFactory.getOMFactory();
// 命名空间
OMNamespace omNs = fac.createOMNamespace(targetNamespace, "");
OMElement methodElement = fac.createOMElement(method, omNs);
// 为方法添加参数,createOMElement方法的第一个参数可以不和目标接口的入参形参名相同
for (Object obj : args) {
OMElement param = fac.createOMElement("param", omNs);
param.setText(String.valueOf(obj));
methodElement.addChild(param);
}
methodElement.build();
OMElement result = sender.sendReceive(methodElement);
return result.getFirstElement().getText();
}
}