webservice soap

package com.irootech.customercloud.common.util.ws;

import com.google.common.collect.Maps;
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 javax.xml.namespace.QName;
import java.util.Map;

public class Axis2InterfaceUtils {

    public static Object getRes(String xmlString, String callFunctionName){
        try {
            RPCServiceClient client = new RPCServiceClient ();
            Options options = client.getOptions();

            // 指定调用WebService的URL
            EndpointReference targetEPR = new EndpointReference("http://10.0.15.114:8080/WebService/services/GetKeyAccessoryList?wsdl");
            options.setTo(targetEPR);
            //options.setAction("命名空间/WS 方法名");
           options.setAction("urn:getKeyAccessory");

            /*options.setExceptionToBeThrownOnSOAPFault(true);
            options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);

            OMElement result = sender.sendReceive(payload);
            String response = result.getFirstElement().getText();
            System.out.println("if login is valid: " + response);*/

            // 指定IncaInsiderService方法的参数值
            Object[] parmas = new Object[] { xmlString.toString()};
            // 指定IncaInsiderService方法返回值的数据类型的Class对象
            Class<?>[] classes = new Class[] { String.class };
            // 指定要调用的IncaInsiderService方法及WSDL文件的命名空间
            QName opAddEntry = new QName("http://keypart.ws.newmes.sany.com", callFunctionName);
            //1 调用IncaInsiderService方法并输出该方法的返回值
            Object[] str = client.invokeBlocking(opAddEntry, parmas, classes);
            System.out.println(str[0]+"1============");
            // 2 传递参数,调用服务,获取服务返回结果集
            OMElement element = client.invokeBlocking(opAddEntry, parmas);
            //值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。
            //我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果
            String result = element.getFirstElement().getText();
            System.out.println(result+"2============");

            return str[0];
        } catch (AxisFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static String getXmlData(String bodyKey, Map<String, String> map) {
        StringBuffer sb = new StringBuffer();
        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        sb.append("<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:key='http://keypart.ws.newmes.sany.com' xmlns:xsd='http://keypart.ws.newmes.sany.com/xsd'>");
        sb.append("<soap:Header/>");
        sb.append("<soap:Body>");
        sb.append("<key:getKeyAccessory>");

        sb.append("<"+bodyKey+">");
        for(String key: map.keySet()) {
            sb.append("<"+key+">" + map.get(key) + "</"+key+">");
        }
        sb.append("</"+bodyKey+">");

        sb.append("</key:getKeyAccessory>");
        sb.append("</soap:Body>");
        sb.append("</soap:Envelope>");
        return sb.toString();
    }

    public static void main(String[] args) {
        //xmlString的格式
        Map<String, String> param = Maps.newHashMap();
        param.put("xsd:VINNo", "LFCNMF5P7K2000888");
        param.put("xsd:factory", "3110");

        String xmlString = Axis2InterfaceUtils.getXmlData("key:inParam", param);
     System.out.println(xmlString);
        System.out.println(Axis2InterfaceUtils.getRes(xmlString.toString(), "getKeyAccessory"));
    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值