axis2调用WSDL接口

public static JSONObject sendWsdl(String url,String xmlStr){
        JSONObject res=new JSONObject();
        try {
            String endpoint = url.replace("?wsdl","");//不需要传?wsdl
            //直接引用远程的wsdl文件
            Options options = new Options();
            options.setTo(new EndpointReference(endpoint));
            options.setAction(targetNamespace+methodName);//解决可能出现的错误:服务器未能识别 HTTP 头 SOAPAction 的值: 。
        
ServiceClient sender
= new ServiceClient(); sender.setOptions(options); OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://localhost/services/IRService","");//参数1(uri)=即为wsdl文档的targetNamespace;参数2(prefix)=可不填 OMElement method = fac.createOMElement("createAuto",omNs);//方法名 OMElement in0 = fac.createOMElement("in0", omNs);//方法参数 in0.setText(xmlStr);//参数值 method.addChild(in0);//添加参数 OMElement resultEle = sender.sendReceive(method);//调用wsdl System.out.println("调用接口结果:"+resultEle.toString()); }

加入maven依赖:

<!--axis2 begin -->
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-spring</artifactId>
   <version>1.7.8</version>
   <exclusions>
      <exclusion>
         <groupId>javax.servlet</groupId>
         <artifactId>servlet-api</artifactId>
      </exclusion>
   </exclusions>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-transport-http</artifactId>
   <version>1.7.8</version>
   <exclusions>
      <exclusion>
         <groupId>javax.servlet</groupId>
         <artifactId>servlet-api</artifactId>
      </exclusion>
   </exclusions>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-transport-local</artifactId>
   <version>1.7.8</version>
   <exclusions>
      <exclusion>
         <groupId>javax.servlet</groupId>
         <artifactId>servlet-api</artifactId>
      </exclusion>
   </exclusions>
</dependency>
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2-xmlbeans</artifactId>
   <version>1.7.8</version>
   <exclusions>
      <exclusion>
         <groupId>javax.servlet</groupId>
         <artifactId>servlet-api</artifactId>
      </exclusion>
   </exclusions>
</dependency>

 完整示例:

 

package com.movittech.movite.kefu.model.service;

import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.impl.util.json.XML;
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.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.NamedValue;
import org.apache.axis2.transport.http.HTTPConstants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 完整test
 */
public class Test {


    public static void main(String[] args) throws Exception{
        String url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";//url地址
        String targetNamespace="http://WebXml.com.cn/";//命名空间
        String methodName="getSupportCity";//方法名
        //请求参数
        List<Map<String,String>> params=new ArrayList<>();
        Map<String,String> params0=new HashMap<String,String>();
        params0.put("byProvinceName", "四川");
        params.add(params0);

        //请求头参数
        List<NamedValue> paramHeaders=new ArrayList<NamedValue>();
        //paramHeaders.add(new NamedValue("ClientId", "123"));
        //paramHeaders.add(new NamedValue("OperationCode", "456"));

        JSONObject res=sendWsdl(url,targetNamespace,methodName,params,paramHeaders);
        System.out.println("调用返回结果:"+res);
    }

    /***
     * 调用Wsdl接口
     * @param url 接口地址,可不加?wsdl
     * @param targetNamespace 命名空间
     * @param methodName 方法名
     * @param params 参数
     * @param paramHeaders 请求头参数
     * @return {"head":"S=成功;F=失败","msg":"描述信息","data":"返回数据"}
     */
    public static JSONObject sendWsdl(String url,String targetNamespace,String methodName,List<Map<String,String>> params,List<NamedValue> paramHeaders){
        JSONObject res=new JSONObject();
        try {
            String endpoint = url.replace("?wsdl","");
            //直接引用远程的wsdl文件
            Options options = new Options();
            options.setTo(new EndpointReference(endpoint));
            options.setAction(targetNamespace+methodName);//解决可能出现的错误:服务器未能识别 HTTP 头 SOAPAction 的值: 。
            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);

            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMNamespace omNs = fac.createOMNamespace(targetNamespace,"");//参数1(uri)=即为wsdl文档的targetNamespace;参数2(prefix)=可不填
            OMElement method = fac.createOMElement(methodName,omNs);//方法名
            //设置参数
            if(params!=null && params.size()>0){
                for(int i=0;i<params.size();i++){
                    Map<String,String> map=params.get(i);
                    for(Map.Entry<String, String> entry : map.entrySet()){
                        String mapKey = entry.getKey();
                        String mapValue = entry.getValue();
                        OMElement in = fac.createOMElement(mapKey, omNs);//参数名
                        in.setText(mapValue);//参数值
                        method.addChild(in);
                    }
                }
            }

            //设置请求头参数
            List<NamedValue> headers = (List<NamedValue>) sender.getOptions().getProperty(HTTPConstants.HTTP_HEADERS);
            if (headers == null){
                headers = new ArrayList<NamedValue>();
            }
            if(paramHeaders!=null && paramHeaders.size()>0){
                headers.addAll(paramHeaders);
            }
            sender.getOptions().setProperty(HTTPConstants.HTTP_HEADERS, headers);

            OMElement resultEle = sender.sendReceive(method);
            System.out.println("调用wsdl接口结果:"+resultEle);

            String result =resultEle.toString();

            //xml转成json格式
            org.activiti.engine.impl.util.json.JSONObject jSONObject= XML.toJSONObject(result);

            //设置返回数据
            res.put("head","S");
            res.put("msg","调用wsdl接口成功");
            res.put("data",JSONObject.parseObject(jSONObject.toString()));
        }catch (Exception e) {
            e.printStackTrace();
            res.put("head","F");
            res.put("msg","调用wsdl接口失败:"+ e.getMessage());
        }
        return res;
    }


}

 备注:sendReceive方法只返回了第一个子元素,要取到全部数据需要自己写个继承类。

 

 

posted @ 2019-06-21 11:07  迢迢  阅读(4400)  评论(0编辑  收藏  举报