Axis2是新一点Axis,基于新的体系结构进行了全新编写,有更强的灵活性并可扩展到新的体系结构。文中demo所使用到的软件版本:Java 1.8.0_191、Axis2 1.7.9。
1、准备
2、调用
2.1、AXIOM方式
AXIOM即AXis对象模型(AXis Object Model)是Apache Axis2的XML对象模型,可以大幅提升Axis2的性能。
/*** axiom方式
*@paramparam*/
public static voidaxiom(String param) {try{
ServiceClient client= newServiceClient();
Options options=client.getOptions();
EndpointReference endpointReference= newEndpointReference(url);
options.setTo(endpointReference);//设置SOAPAction
options.setAction("http://webxml.com.cn/toTraditionalChinese");//设置soap版本
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);//options.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
OMFactory factory=OMAbstractFactory.getOMFactory();
OMNamespace namespace= factory.createOMNamespace(targetNamespace, "");
OMElement method= factory.createOMElement("toTraditionalChinese", namespace);
OMElement value= factory.createOMElement("sText", namespace);
value.addChild(factory.createOMText(value, param));
method.addChild(value);
method.build();
OMElement result=client.sendReceive(method);
System.out.println(result);
System.out.println(result.getFirstElement().getText());
}catch(Exception e) {
e.printStackTrace();
}
}
5.2、ADB方式
ADB即Axis Data Binding,Axis2自己的数据绑定方法。
2.2.1、用wsdl2java生成代码
%AXIS2_HOME%\bin\wsdl2java.bat -uri http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl -p com.inspur.ws.axis2.adb -d adb -s -o temp
-d 指定数据绑定方式
-p 指定包名
-o 指定代码生成的目录,指定的目录就生成在当前目录下,windows下指定绝对路径不生效(如:d:/temp)
命令会生成一个java文件:
2.2.2、生成代码调用WebService
/*** adb方式
*@paramparam*/
public static voidadb(String param) {try{
com.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub stub= newcom.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub();
com.inspur.ws.axis2.adb.Traditiona