1、先在pom.xml中配置Axis2,引入Axis2的jar包
<properties> <axis2.version>1.7.8</axis2.version> </properties> <!--axis2 begin--> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-spring</artifactId> <version>${axis2.version}</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-transport-http</artifactId> <version>${axis2.version}</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-transport-local</artifactId> <version>${axis2.version}</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-xmlbeans</artifactId> <version>${axis2.version}</version> </dependency>
2、import命名空間
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;
3、調用Webservice
public void getPrivilege(String userUid) throws AxisFault { ServiceClient serviceClient = new ServiceClient(); //創建服務地址WebService的URL,注意不是WSDL的URL String url = "http://localhost:8100/GetBorkBillService.asmx"; EndpointReference targetEPR = new EndpointReference(url); Options options = serviceClient.getOptions(); options.setTo(targetEPR); //確定調用方法(wsdl 命名空間地址 (wsdl文檔中的targetNamespace) 和 方法名稱 的組合) options.setAction("http://tempuri.org/GetMyBillInfo"); OMFactory fac = OMAbstractFactory.getOMFactory(); /* * 指定命名空間,參數: * uri--即為wsdl文檔的targetNamespace,命名空間 * perfix--可不填 */ OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", ""); // 指定方法 OMElement method = fac.createOMElement("GetMyBillInfo", omNs);
//為方法指定參數 OMElement theRegionCode = fac.createOMElement("type", omNs); theRegionCode.setText("1"); OMElement theRegionCode1 = fac.createOMElement("userUid", omNs); theRegionCode1.setText("lujianghai"); method.addChild(theRegionCode); method.addChild(theRegionCode1); method.addChild(method); method.build(); //遠程調用web服務 OMElement result = serviceClient.sendReceive(method); System.out.println(result); }