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

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

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

org.apache.axis

axis

1.4

commons-discovery

commons-discovery

0.2

commons-logging

commons-logging

org.apache.axis

axis-jaxrpc

1.4

org.apache.axis

axis-saaj

1.4

wsdl4j

wsdl4j

1.4

org.apache.cxf

cxf-rt-frontend-jaxws

3.1.12

org.apache.cxf

cxf-rt-transports-http

3.1.12

wsdl4j

wsdl4j

1.6.3

org.apache.cxf

cxf-rt-transports-http-jetty

3.1.12

com.google.code.gson

gson

2.8.4

com.hikvision.ga

artemis-http-client

1.1.3

com.alibaba

fastjson

1.2.71

commons-logging

commons-logging

1.1.3

provided

org.apache.httpcomponents

httpclient

4.5.3

cn.hutool

hutool-core

5.3.10

cn.hutool

hutool-http

5.3.10

二、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 paramMap = new HashMap();

paramMap.put("netid", "15.3.9");

List> 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);

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐