java使用axios 或者 hutool-http包下的SoapClient 实现webservice服务调用

在项目开发过程中,有时会用到 webservice 服务,今天给大家带来一个简单好用的 soap请求 工具类。

里面用到了两个第三方依赖,大家可以自主进行选择,推荐使用hutool-http包的SoapClient

一、项目pom文件添加相关依赖

 <!--axis依赖-->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-saaj</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.12</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.12</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.4</version>
        </dependency>
        <dependency>
            <groupId>com.hikvision.ga</groupId>
            <artifactId>artemis-http-client</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.71</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-http -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-core</artifactId>
            <version>5.3.10</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-http</artifactId>
            <version>5.3.10</version>
        </dependency>

二、AxisUtil.java

package com.sf.map.tool.station.util;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.webservice.SoapClient;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.utils.StringUtils;
import org.apache.axis.utils.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import javax.xml.xpath.XPathConstants;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 80004819
 * @ClassName:
 * @Description:
 * @date 2020年07月20日 18:19:13
 */
@Slf4j
public class AxisUtil {
    /**
     * @param endpoint        //WSDL的地址
     * @param targetNamespace //命名空间
     * @param method          //具体调用的方法名
     * @param soapActionURI   //具体方法的调用URI
     * @param paramNames      //调用接口的参数的名字
     * @param paramValues     //调用接口的参数的值
     * @return
     */
    public static Object callWebService(String endpoint, String targetNamespace, String method, String soapActionURI, String[] paramNames, String[] paramValues) {
        try {
            //字符集
            final String encodingStyle = "utf-8";
            org.apache.axis.client.Service service = new org.apache.axis.client.Service();
            Call call = (Call) service.createCall();
//            call.setEncodingStyle(encodingStyle);//设置传入服务端的字符集格式如utf-8等
            call.setTimeout(new Integer(20000));  //设置超时时间
            call.setTargetEndpointAddress(new java.net.URL(endpoint));  //设置目标接口的地址
            if (!StringUtils.isEmpty(soapActionURI)) {
                call.setSOAPActionURI(soapActionURI);
                call.setUseSOAPAction(true);
            }
            call.setOperationName(new QName(targetNamespace, method));// 具体调用的方法名,可以由接口提供方告诉你,也可以自己从WSDL中找
//            call.setOperationName(method);
            // 接口的参数
            call.addParameter(new QName(targetNamespace, paramNames[0]),
                    XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            call.setReturnType(XMLType.XSD_STRING);// 设置返回类型  ,如String
            call.setReturnClass(String.class); //返回字符串数组类型
            // 给方法传递参数,并且调用方法 ,如果无参,则new Obe
//            String[] result = (String[]) call.invoke(new Object[]{paramValues[0]});
//            String[] result = (String[]) call.invoke(new Object[]{});
            Object result = call.invoke(new Object[]{paramValues[0]});
            // 打印返回值
//            System.out.println("result is " + result.toString());
            return result;
        } catch (ServiceException | MalformedURLException | RemoteException e) {
            log.error("webservice接口调用异常", e);
        } catch (Exception e) {
            log.error("webservice接口调用异常", e);
        }
        return null;
    }


    private static final String RESPONSE_PREFIX = "//soap11env:Envelope/soap11env:Body/";


    /**
     * 使用Soapclient 调用webservice接口
     *
     * @param endpoint
     * @param targetNamespace
     * @param method
     * @param soapActionURI
     * @param paramNames
     * @param paramValues
     * @param xpath_suffix
     * @return
     */
    public static Object callWebServiceBySoap(String endpoint, String targetNamespace, String method, String soapActionURI, String paramNames, String paramValues, String xpath_suffix) {
        try {
            log.info("webservice接口请求地址:"+endpoint);
            SoapClient client = SoapClient.create(endpoint)
                    // 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
                    .setMethod(method, targetNamespace)
                    // 设置参数,此处自动添加方法的前缀:web
                    .setParam(paramNames, paramValues);
            // 发送请求,参数true表示返回一个格式化后的XML内容
            String xmlRes = client.send(true);
            Document document = XmlUtil.readXML(xmlRes);
            Object byXPath = XmlUtil.getByXPath(RESPONSE_PREFIX + xpath_suffix, document, XPathConstants.STRING);
            log.info("接口地址:{},方法:{},响应结果:{}",endpoint,method,byXPath);
            return byXPath;
        } catch (Exception e) {
            log.error("webservice接口调用异常", e);
        }
        return null;
    }


    public static void main(String[] args) {
//        AxisUtil.callWebService("http://10.82.232.231:9000/victel/CityTopService?WSDL", "http://nm.pdt.org.cn", "topQuery", "http://schemas.xmlsoap.org/wsdl/soap/", new String[]{"input"},new String[]{""} );
        /**
         * 基站基础数据调用
         */
//        SoapClient client = SoapClient.create("http://10.82.232.231:9000/victel/CityTopService?WSDL")
//                // 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
//                .setMethod("topQuery", "http://nm.pdt.org.cn")
//                // 设置参数,此处自动添加方法的前缀:web
//                .setParam("input", "");
//        // 发送请求,参数true表示返回一个格式化后的XML内容
//        String xmlRes = client.send(true);
//        Document document = XmlUtil.readXML(xmlRes);
//        Object byXPath = XmlUtil.getByXPath(RESPONSE_PREFIX+"tns:topQueryResponse/tns:topList", document, XPathConstants.STRING);
//        Console.log(byXPath);


        /**
         * 基站链路状态调用
         */
//        SoapClient client = SoapClient.create("http://10.82.232.231:9000/victel/CustomMonitorService?WSDL")
//                // 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
//                .setMethod("currLineStateQuery", "http://nm.pdt.org.cn")
//                // 设置参数,此处自动添加方法的前缀:web
//                .setParam("input", "");
//        // 发送请求,参数true表示返回一个格式化后的XML内容
//        String xmlRes = client.send(true);
//        Console.log(xmlRes);
//        Document document = XmlUtil.readXML(xmlRes);
//        Object byXPath = XmlUtil.getByXPath(RESPONSE_PREFIX+"tns:currLineStateQueryResponse/tns:currLineStateList", document, XPathConstants.STRING);
//        Console.log(byXPath);


        /**
         * 基站呼叫状态调用
         */
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("netid", "15.3.9");
        List<Map<String, String>> netIdList = new ArrayList<>();
        netIdList.add(paramMap);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("idArray", netIdList);
        String value = jsonObject.toJSONString();

        SoapClient client = SoapClient.create("http://10.82.232.231:9000/victel/CustomMonitorService?WSDL")
                // 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
                .setMethod("currBsCallQuery", "http://nm.pdt.org.cn")
                // 设置参数,此处自动添加方法的前缀:web
                .setParam("netId", value);
        // 发送请求,参数true表示返回一个格式化后的XML内容
        String xmlRes = client.send(true);
        Console.log(xmlRes);
        Document document = XmlUtil.readXML(xmlRes);
        Object byXPath = XmlUtil.getByXPath(RESPONSE_PREFIX + "tns:currBsCallQueryResponse/tns:currBsCallList", document, XPathConstants.STRING);
        Console.log(byXPath);
    }

}

猜你喜欢

转载自blog.csdn.net/qq_31905135/article/details/107817730