使用Axis2调用WebService服务

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); }

 

posted @ 2019-09-10 17:06  人生只若初见  阅读(6185)  评论(0编辑  收藏  举报