-
Java调用WebService方法总结(5)--Axis2调用WebService
Axis2是新一点Axis,基于新的体系结构进行了全新编写,有更强的灵活性并可扩展到新的体系结构。文中demo所使用到的软件版本:Java 1.8.0_191、Axis2 1.7.9。
1、准备
参考Java调用WebService方法总结(1)--准备工作
2、调用
2.1、AXIOM方式
AXIOM即AXis对象模型(AXis Object Model)是Apache Axis2的XML对象模型,可以大幅提升Axis2的性能。
/** * axiom方式 * @param param */ public static void axiom(String param) { try { ServiceClient client = new ServiceClient(); Options options = client.getOptions(); EndpointReference endpointReference = new EndpointReference(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方式 * @param param */ public static void adb(String param) { try { com.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub stub = new com.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub(); com.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub.ToTraditionalChinese request = new com.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub.ToTraditionalChinese(); request.setSText(param); com.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub.ToTraditionalChineseResponse response = stub.toTraditionalChinese(request); System.out.println("adb结果:" + response.getToTraditionalChineseResult()); } catch (Exception e) { e.printStackTrace(); } }
2.3、XMLBeans方式
使用XML Beans数据绑定方式。
2.3.1、用wsdl2java生成代码
%AXIS2_HOME%\bin\wsdl2java.bat -uri http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl -p com.inspur.ws.axis2.xmlbeans -d xmlbeans -s -o temp
与adb方式相比只有-d参数的值不同。
这种方式会生成好多文件,src下有三个文件目录,里面有好多文件就不一一展开了:
resources目录下有:
注:需把schemaorg_apache_xmlbeans放到classpath中否则会报错误:找不到相关类。
2.3.2、生成代码调用WebService
/** * xmlbeans方式 * @param param */ public static void xmlbeans(String param) { try { com.inspur.ws.axis2.xmlbeans.TraditionalSimplifiedWebServiceStub stub = new com.inspur.ws.axis2.xmlbeans.TraditionalSimplifiedWebServiceStub(); ToTraditionalChineseDocument document = ToTraditionalChineseDocument.Factory.newInstance(); ToTraditionalChineseDocument.ToTraditionalChinese request = document.addNewToTraditionalChinese(); request.setSText(param); ToTraditionalChineseResponseDocument response = stub.toTraditionalChinese(document); System.out.println("xmlbeans:" + response.getToTraditionalChineseResponse().getToTraditionalChineseResult()); } catch (Exception e) { e.printStackTrace(); } }
2.4、JiBX方式
使用JiBX数据绑定方式。
2.4.1、用wsdl2java生成代码
%AXIS2_HOME%\bin\wsdl2java.bat -uri http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl -p com.inspur.ws.axis2.jibx -d jibx -s -uw -o temp
与adb方式相比除了-d参数值不同,还需增加-uw参数。
命令会生成两个文件:
2.4.2、生成代码调用WebService
/** * jibx方式 * @param param */ public static void jibx(String param) { try { com.inspur.ws.axis2.jibx.TraditionalSimplifiedWebServiceStub stub = new com.inspur.ws.axis2.jibx.TraditionalSimplifiedWebServiceStub(); String result = stub.toTraditionalChinese(param); System.out.println("jibx结果:" + result); } catch (Exception e) { e.printStackTrace(); } }
2.5、JAXBRI方式
使用JAXBRI数据绑定方式。
2.5.1、用wsdl2java生成代码
%AXIS2_HOME%\bin\wsdl2java.bat -uri http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl -p com.inspur.ws.axis2.jaxbri -d jaxbri -s -o temp
与adb方式相比只有-d参数值不同。
命令会生成两个目录,一个值以命名空间生成的目录cn.com.webxml,一个是指定的包名com.inspur.ws.axis2.jaxbri。
2.5.2、生成代码调用WebService
/** * jaxbri方式 * @param param */ public static void jaxbri(String param) { try { com.inspur.ws.axis2.jaxbri.TraditionalSimplifiedWebServiceStub stub = new com.inspur.ws.axis2.jaxbri.TraditionalSimplifiedWebServiceStub(); cn.com.webxml.ToTraditionalChinese toTraditionalChinese = new cn.com.webxml.ToTraditionalChinese(); toTraditionalChinese.setSText(param); String result = stub.toTraditionalChinese(toTraditionalChinese).getToTraditionalChineseResult(); System.out.println("jaxbri结果:" + result); } catch (Exception e) { e.printStackTrace(); } }
2.6、小结
AXIOM方式不需要生成额外代码,ADB和JiBX方式生成的代码较少,JAXBRI生成代码较多,XMLBeans生成的代码最多比较啰嗦;生成代码是还可以用-t参数来生成测试用例。ADB、XMLBeans、JiBX和JAXBRI方式生成的代码默认使用Soap1.2协议,没有生成两个版本。程序中所使用的jar包在%AXIS2_HOME%/lib中都可以找到,可以根据需要添加。下面是完整的demo,要测试某一个方法可以把其他的先注释掉。
package com.inspur.ws; 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.axiom.soap.SOAP11Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; /** * * Axis2调用WebService * */ public class Axis2 { private static String url = "http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl"; private static String targetNamespace = "http://webxml.com.cn/"; /** * axiom方式 * @param param */ public static void axiom(String param) { try { ServiceClient client = new ServiceClient(); Options options = client.getOptions(); EndpointReference endpointReference = new EndpointReference(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("axiom结果:" + result.getFirstElement().getText()); } catch (Exception e) { e.printStackTrace(); } } /** * adb方式 * @param param */ public static void adb(String param) { try { com.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub stub = new com.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub(); com.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub.ToTraditionalChinese request = new com.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub.ToTraditionalChinese(); request.setSText(param); com.inspur.ws.axis2.adb.TraditionalSimplifiedWebServiceStub.ToTraditionalChineseResponse response = stub.toTraditionalChinese(request); System.out.println("adb结果:" + response.getToTraditionalChineseResult()); } catch (Exception e) { e.printStackTrace(); } } /** * xmlbeans方式 * @param param */ public static void xmlbeans(String param) { try { com.inspur.ws.axis2.xmlbeans.TraditionalSimplifiedWebServiceStub stub = new com.inspur.ws.axis2.xmlbeans.TraditionalSimplifiedWebServiceStub(); cn.com.webxml.ToTraditionalChineseDocument document = cn.com.webxml.ToTraditionalChineseDocument.Factory.newInstance(); cn.com.webxml.ToTraditionalChineseDocument.ToTraditionalChinese request = document.addNewToTraditionalChinese(); request.setSText(param); cn.com.webxml.ToTraditionalChineseResponseDocument response = stub.toTraditionalChinese(document); System.out.println("xmlbeans结果:" + response.getToTraditionalChineseResponse().getToTraditionalChineseResult()); } catch (Exception e) { e.printStackTrace(); } } /** * jibx方式 * @param param */ public static void jibx(String param) { try { com.inspur.ws.axis2.jibx.TraditionalSimplifiedWebServiceStub stub = new com.inspur.ws.axis2.jibx.TraditionalSimplifiedWebServiceStub(); String result = stub.toTraditionalChinese(param); System.out.println("jibx结果:" + result); } catch (Exception e) { e.printStackTrace(); } } /** * jaxbri方式 * @param param */ public static void jaxbri(String param) { try { com.inspur.ws.axis2.jaxbri.TraditionalSimplifiedWebServiceStub stub = new com.inspur.ws.axis2.jaxbri.TraditionalSimplifiedWebServiceStub(); cn.com.webxml.ToTraditionalChinese toTraditionalChinese = new cn.com.webxml.ToTraditionalChinese(); toTraditionalChinese.setSText(param); String result = stub.toTraditionalChinese(toTraditionalChinese).getToTraditionalChineseResult(); System.out.println("jaxbri结果:" + result); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { axiom("小学"); adb("大学"); xmlbeans("小学"); jibx("大学"); jaxbri("大学"); } }
来源:https://www.cnblogs.com/wuyongyin/p/11854806.html