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);
- }
来源: http://www.bubuko.com/infodetail-3191769.html