java通过axis2调取webservice接口

1 首先pom.xml引入axis2 maven配置

        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-codegen</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-local</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>1.6.1</version>
        </dependency>

2 自己写个webservice 服务端接口
比如:http://localhost:8080/webServer/cxf/cxftest?wsdl

3 调用webservice接口
(1)调不带@WebParam参数的方法

    try {
            String serviceUrl = "http://192.168.1.235:8080/webServer/cxf/cxftest?wsdl";
            String tns = "http://service.fxl.com/";
            String methodName = "getPass";
            //使用RPC方式调用WebService  
            RPCServiceClient serviceClient = new RPCServiceClient();  
            Options options = serviceClient.getOptions();  
            //设置2秒超时  
            options.setTimeOutInMilliSeconds(2000L);  
            //指定调用WebService的URL  
            EndpointReference targetEPR = new EndpointReference(serviceUrl);  
            options.setTo(targetEPR);  

            //指定接口方法的参数值  
            Object[] opAddEntryArgs = new Object[] {"admin", 1};
            //指定方法返回值的数据类型的Class对象  
            Class[] classes = new Class[] { String.class };  
            //指定调用的方法及WSDL文件的命名空间  QName("targetNamespace","method Name");  
            QName qName = new QName(tns, methodName);  
            //调用getVersioin方法并输出该方法的返回值,  
            //返回对象是一个Object的数组,拿数组的第一个值,转换强转即可  
            Object result = serviceClient.invokeBlocking(qName, opAddEntryArgs, classes)[0];
            //OMElementImpl ele =  (OMElementImpl) result;
            System.out.println("返回值result=" + result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

(1)调带@WebParam参数的方法

    try {
            String serviceUrl = "http://192.168.1.235:8080/webServer/cxf/cxftest?wsdl";
            String tns = "http://service.fxl.com/";
            String methodName = "getAuth";

            //使用RPC方式调用WebService  
            RPCServiceClient serviceClient = new RPCServiceClient();  
            Options options = serviceClient.getOptions();  
            //设置2秒超时  
            options.setTimeOutInMilliSeconds(2000L);  
            //指定调用WebService的URL  
            EndpointReference targetEPR = new EndpointReference(serviceUrl);  
            options.setTo(targetEPR);  

            Map<String, String> params = new HashMap<String, String>();
            params.put("userName", "admin");
            params.put("password", "123456");
            //添加参数并获取方法
            OMElement method = getMethod(tns, methodName, params);
            OMElement result = serviceClient.sendReceive(method);
            //result就是获取的数据,剩下需要自己解析
            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    /**
     * 获取请求方法
     * @param tns 命名空间
     * @param methodName 方法名
     * @param params 参数
     * @return
     */
    private static OMElement getMethod(String tns, String methodName, Map<String, String> params){
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(tns, "");
        OMElement method = fac.createOMElement(methodName, omNs);
        if(params != null) {
            for (String key : params.keySet()) {
                OMElement param = fac.createOMElement(key, null);
                param.addChild(fac.createOMText(param, params.get(key)));
                method.addChild(param);
            }
            method.build();
        }
        return method;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值