Axis2是新一點Axis,基於新的體系結構進行了全新編寫,有更強的靈活性並可擴展到新的體系結構。文中demo所使用到的軟件版本:Java 1.8.0_19一、Axis2 1.7.9。html
參考Java調用WebService方法總結(1)--準備工做java
AXIOM即AXis對象模型(AXis Object Model)是Apache Axis2的XML對象模型,能夠大幅提高Axis2的性能。web
/** * 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(); } }
ADB即Axis Data Binding,Axis2本身的數據綁定方法。apache
%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 指定數據綁定方式windows
-p 指定包名ide
-o 指定代碼生成的目錄,指定的目錄就生成在當前目錄下,windows下指定絕對路徑不生效(如:d:/temp)性能
命令會生成一個java文件:測試
/** * 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(); } }
使用XML Beans數據綁定方式。ui
%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參數的值不一樣。url
這種方式會生成好多文件,src下有三個文件目錄,裏面有好多文件就不一一展開了:
resources目錄下有:
注:需把schemaorg_apache_xmlbeans放到classpath中不然會報錯誤:找不到相關類。
/** * 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(); } }
使用JiBX數據綁定方式。
%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參數。
命令會生成兩個文件:
/** * 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數據綁定方式。
%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。
/** * 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(); } }
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("大學"); } }