Axis2调用WebService服务的3种方式(rpc调用不好用)

第一,RPC方式,不生成客户端代码,引入相应的axis2的jar包(不好用)
注意:暂时没有成功调用,没有参数传递时远程调用成功,当有参数传递时远程调用失败;

package com.ming.axis2;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.junit.Test;

/**
 * 方法一: 应用rpc的方式调用 这种方式就等于远程调用, 即通过url定位告诉远程服务器,告知方法名称,参数等, 调用远程服务,得到结果。
 * 使用org.apache.axis2.rpc.client.RPCServiceClient类调用WebService;
 * 
 * 【注】:
 * 如果被调用的WebService方法有返回值 应使用 invokeBlocking 方法 该方法有三个参数
 * 第一个参数的类型是QName对象,表示要调用的方法名;
 * 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[],
 * 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{};
 * 第三个参数表示WebService方法的 返回值类型的Class对象,参数类型为Class[]。
 * 
 * 如果被调用的WebService方法没有返回值 应使用 invokeRobust方法,
 * 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。
 * 
 */
public class MobileClientRPC {

    /**
     * 
    * @Title: testRPCClient
    * @Description: TODO(rpc远程调用,失败,无法正确传参)
    * @return void    返回类型
     */
    @Test
    public void testRPCClient() {
        try {
            // 使用RPC方式调用WebService
            RPCServiceClient serviceClient = new RPCServiceClient();
            // 创建WSDL的URL,注意不是服务地址
            String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";
            // 指定调用WebService的URL
            EndpointReference targetEPR = new EndpointReference(url);
            Options options = serviceClient.getOptions();
            // 确定目标服务地址
            options.setTo(targetEPR);
            // 确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)
            options.setAction("http://WebXml.com.cn/getMobileCodeInfo");
            // 指定方法的参数值
            Object[] parameters = new Object[] {"1866666666", ""};

            // 创建服务名称
            // 1.namespaceURI - 命名空间地址 (wsdl文档中的targetNamespace)
            // 2.localPart - 服务视图名 (wsdl文档中operation的方法名称,例如<wsdl:operation name="getMobileCodeInfo">)
            QName qname = new QName("http://WebXml.com.cn/", "getMobileCodeInfo");


            // 调用方法一 传递参数,调用服务,获取服务返回结果集
            OMElement element = serviceClient.invokeBlocking(qname, parameters);
            System.out.println(element);
            /*
             * 值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。
             * 我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果
             */
            String result = element.getFirstElement().getText();
            System.out.println(result);


            // 调用方法二 getPrice方法并输出该方法的返回值
            // 指定方法返回值的数据类型的Class对象
            Class[] returnTypes = new Class[] {String.class};
            Object[] response = serviceClient.invokeBlocking(qname, parameters, returnTypes);
            String r = (String) response[0];
            System.out.println(r);

        } catch (AxisFault e) {
            e.printStackTrace();
        }
    }


    /**
     * 
    * @Title: Weather
    * @Description: TODO(rpc远程调用,成功,没有参数能够成功)
    * @return void    返回类型
    * @throws AxisFault
     */
    @Test
    public void Weather() throws AxisFault{    
        //使用RPC方式调用WebService         
        RPCServiceClient serviceClient = new RPCServiceClient();    
        Options options = serviceClient.getOptions();    
        //指定调用WebService的URL    
        EndpointReference targetEPR = 
                new EndpointReference("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");    
        options.setTo(targetEPR);
        options.setAction("http://WebXml.com.cn/getRegionProvince");
        //指定方法的参数值    
        Object[] opAddEntryArgs = new Object[] {};    
        //指定要调用的方法及WSDL文件的命名空间    
        QName opAddEntry = new QName("http://WebXml.com.cn/", "getRegionProvince");    
        //调用法并输出该方法的返回值    
        System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs));  
    }

    /**
     * 
    * @Title: Weather1
    * @Description: TODO(rpc远程调用,失败,单个参数失败)
    * @return void    返回类型
     */
    @Test
    public void Weather1() {    
        try {
            //使用RPC方式调用WebService         
            RPCServiceClient serviceClient = new RPCServiceClient();    
            Options options = serviceClient.getOptions();    
            //指定调用WebService的URL    
            EndpointReference targetEPR = 
                    new EndpointReference("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");    
            options.setTo(targetEPR);
            options.setAction("http://WebXml.com.cn/getSupportCityDataset");
            //指定方法的参数值    
            Object[] opAddEntryArgs = new Object[] {"北京"};   
            //指定要调用的方法及WSDL文件的命名空间    
            QName opAddEntry = new QName("http://WebXml.com.cn/", "getSupportCityDataset");    
            //调用法并输出该方法的返回值    
            System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs));
        } catch (AxisFault e) {
            e.printStackTrace();
        }  
    }
}

第二,应用document方式调用 用ducument方式应用现对繁琐而灵活,引入相应的axis2的jar包。现在用的比较多。
即使用org.apache.axis2.client.ServiceClient类进行远程调用web服务,不生成客户端(推荐使用)

package com.ming.axis2;

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;
import org.junit.Test;

/**
 * 
* @ClassName: MobileClientDoc 
* @Description: TODO
* 方法二: 应用document方式调用 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合
* 即使用org.apache.axis2.client.ServiceClient类进行远程调用web服务,不生成客户端
*  
* @date 2017年11月9日 下午1:27:17 
*
 */
public class MobileClientDoc {

    /**
     * 
    * @Title: Weather
    * @Description: TODO(document远程调用,成功,没有参数能够成功)
    * @return void    返回类型
    * @throws AxisFault
     */
    @Test
    public void Weather() throws AxisFault{
        ServiceClient serviceClient = new ServiceClient();
        //创建服务地址WebService的URL,注意不是WSDL的URL
        String url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";
        EndpointReference targetEPR = new EndpointReference(url);
        Options options = serviceClient.getOptions();
        options.setTo(targetEPR);
        //确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)
        options.setAction("http://WebXml.com.cn/getRegionProvince");

        OMFactory fac = OMAbstractFactory.getOMFactory();
        /*
         * 指定命名空间,参数:
         * uri--即为wsdl文档的targetNamespace,命名空间
         * perfix--可不填
         */
        OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");
        // 指定方法
        OMElement method = fac.createOMElement("getSupportCityDataset", omNs);
        method.build();

        //远程调用web服务
        OMElement result = serviceClient.sendReceive(method);
        System.out.println(result);
    }


    /**
     * 
    * @Title: Weather1
    * @Description: TODO(document远程调用,成功,没有参数能够成功)
    * @return void    返回类型
     */
    @Test
    public void Weather1() {
        try {
            ServiceClient serviceClient = new ServiceClient();
            //创建服务地址WebService的URL,注意不是WSDL的URL
            String url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";
            EndpointReference targetEPR = new EndpointReference(url);
            Options options = serviceClient.getOptions();
            options.setTo(targetEPR);
            //确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)
            options.setAction("http://WebXml.com.cn/getSupportCityDataset");

            OMFactory fac = OMAbstractFactory.getOMFactory();
            /*
             * 指定命名空间,参数:
             * uri--即为wsdl文档的targetNamespace,命名空间
             * perfix--可不填
             */
            OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");
            // 指定方法
            OMElement method = fac.createOMElement("getSupportCityDataset", omNs);
            // 指定方法的参数
            OMElement theRegionCode = fac.createOMElement("theRegionCode", omNs);
            theRegionCode.setText("北京");
            method.addChild(theRegionCode);
            method.build();

            //远程调用web服务
            OMElement result = serviceClient.sendReceive(method);
            System.out.println(result);

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        }
    }


    /**
     * 
    * @Title: Weather1
    * @Description: TODO(document远程调用,成功,多个参数)
    * @return void    返回类型
     */
    @Test
    public void MobileCodeWS() {
        try {
            ServiceClient serviceClient = new ServiceClient();
            //创建服务地址WebService的URL,注意不是WSDL的URL
            String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
            EndpointReference targetEPR = new EndpointReference(url);
            Options options = serviceClient.getOptions();
            options.setTo(targetEPR);
            //确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)
            options.setAction("http://WebXml.com.cn/getMobileCodeInfo");

            OMFactory fac = OMAbstractFactory.getOMFactory();
            /*
             * 指定命名空间,参数:
             * uri--即为wsdl文档的targetNamespace,命名空间
             * perfix--可不填
             */
            OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");
            // 指定方法
            OMElement method = fac.createOMElement("getMobileCodeInfo", omNs);
            // 指定方法的参数
            OMElement mobileCode = fac.createOMElement("mobileCode", omNs);
            mobileCode.setText("15932582632");
            OMElement userID = fac.createOMElement("userID", omNs);
            userID.setText("");
            method.addChild(mobileCode);
            method.addChild(userID);
            method.build();

            //远程调用web服务
            OMElement result = serviceClient.sendReceive(method);
            System.out.println(result);

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        }
    }
}

第三,用wsdl2java工具,生成客户端方式调用webservice服务
步骤:
(1)引入axis2的相应jar包;
(2)下载axis2-1.7.4-bin.zip,在bin目录中有wsdl2java,用于根据WSDL生成相应的服务端和客户端代码的生成工具;

wsdl2java 用于根据WSDL生成相应的服务端和客户端代码的生成工具。
命令行格式为:WSDL2Java [options] -uri <url or path> : A url or path to a WSDL
例如:
wsdl2java -uri http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl -s -p com.ming.mobile -o build\client

wsdl2java -uri http://localhost:8080/cxfService_0617/services/Hellows?wsdl -s -o build\client

其中常用的options具体如下:
-o <path> : 指定生成代码的输出路径
-a : 生成异步模式的代码
-s : 生成同步模式的代码
-p <pkg> : 指定代码的package名称
-l <languange> : 使用的语言(Java/C) 默认是java
-t : 为代码生成测试用例
-ss : 生成服务端代码 默认不生成
-sd : 生成服务描述文件 services.xml,仅与-ss一同使用
-d <databinding> : 指定databingding,例如,adb,xmlbean,jibx,jaxme and jaxbri
-g : 生成服务端和客户端的代码
-pn <port_name> :WSDL中有多个port时,指定其中一个port
-sn <serv_name> : 选择WSDL中的一个service
-u : 展开data-binding的类
-r <path> : 为代码生成指定一个repository
-ssi : 为服务端实现代码生成接口类
-S : 为生成的源码指定存储路径
-R : 为生成的resources指定存储路径
–noBuildXML : 输出中不生成build.xml文件
–noWSDL : 在resources目录中不生成WSDL文件
–noMessageReceiver : 不生成MessageReceiver类

(3)调用实例如下;

package com.ming.axis2;

import java.rmi.RemoteException;

import org.apache.axis2.AxisFault;
import org.junit.Test;

import com.ming.mobile.MobileCodeWSStub;
import com.ming.mobile.MobileCodeWSStub.GetMobileCodeInfo;
import com.ming.mobile.MobileCodeWSStub.GetMobileCodeInfoResponse;

/**
 * 
* @ClassName: MobileClientWsdl2 
* @Description: TODO(利用axis2插件wsdl2java生成客户端方式调用,只生成一个类,其中含有很多内部类) 
* @date 2017年11月9日 下午2:14:03 
*
 */
public class MobileClientWsdl2 {
    @Test
    public void testCodeClient() {
        try {
            // web服务地址,不是wsdl地址
            String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
            //创建客户端实体类
            MobileCodeWSStub stub = new MobileCodeWSStub(url);
            //创建调用方法的实体,设置参数
            GetMobileCodeInfo getMobileCodeInfo = new GetMobileCodeInfo();
            getMobileCodeInfo.setMobileCode("15932582632");
            getMobileCodeInfo.setUserID("");

            //客户端调用方法,并返回xml信息
            GetMobileCodeInfoResponse mobileCodeInfo = stub.getMobileCodeInfo(getMobileCodeInfo);
            System.out.println(mobileCodeInfo.getGetMobileCodeInfoResult());
        } catch (AxisFault e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
  • 11
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
使用Axis2调用WebService服务需要以下步骤: 1. 下载Axis2库 首先需要下载Axis2库,可以从官方网站或者Maven仓库中获取到Axis2的jar包。下载好后,需要在项目中引入Axis2相关的jar包。 2. 创建客户端代理 使用wsdl2java命令,可以根据服务端的wsdl文件生成客户端代码。命令格式如下: ``` wsdl2java -uri http://localhost:8080/axis2/services/MyService?wsdl -p com.example.client -d /path/to/output ``` 其中,-uri参数指定服务端的wsdl文件地址,-p参数指定生成的客户端代码的包名,-d参数指定生成的客户端代码的输出目录。 生成的客户端代码包括服务端的接口类、接口实现类、客户端代理类等。 3. 创建客户端 创建客户端的代码如下: ``` // 创建服务地址 String url = "http://localhost:8080/axis2/services/MyService"; // 创建服务代理 MyServiceStub stub = new MyServiceStub(url); // 调用服务方法 MyServiceStub.MyMethod request = new MyServiceStub.MyMethod(); request.setParam1("param1"); request.setParam2("param2"); MyServiceStub.MyMethodResponse response = stub.myMethod(request); ``` 其中,url参数指定服务地址,MyServiceStub是客户端代理类,MyMethod是服务端的方法,MyMethodResponse是服务端方法的返回值。 4. 调用服务 调用服务的代码如下: ``` // 创建请求消息 OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://example.com", "ns"); OMElement method = fac.createOMElement("myMethod", omNs); OMElement param1 = fac.createOMElement("param1", omNs); param1.setText("param1"); method.addChild(param1); OMElement param2 = fac.createOMElement("param2", omNs); param2.setText("param2"); method.addChild(param2); // 创建服务客户端 ServiceClient serviceClient = new ServiceClient(); Options options = new Options(); options.setTo(new EndpointReference(url)); serviceClient.setOptions(options); // 调用服务 OMElement response = serviceClient.sendReceive(method); ``` 其中,OMFactory和OMNamespace是Axis2提供的类,用于构建SOAP消息。OMElement是SOAP消息的元素,addChild方法用于添加子元素。ServiceClient是Axis2提供的服务客户端类,Options类用于设置服务调用的选项,包括服务地址、超时时间等。sendReceive方法用于发送请求消息并接收服务端的响应消息。 以上就是使用Axis2调用WebService服务的基本步骤。需要注意的是,调用服务时需要根据服务端的要求构建SOAP消息,具体内容需要根据服务端的接口文档进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值