WebService系列之使用Axis调用第三方wsdl接口
测试工具下载soapui测试
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
pom配置:
<properties>
<!-- axis -->
<axis.version>1.4</axis.version>
<!-- commons-iscovery-->
<commons-discovery.version>0.2</commons-discovery.version>
<!-- wsdl4j -->
<wsdl4j.version>1.6.3</wsdl4j.version>
<!-- slf4j -->
<slf4j.version>1.7.29</slf4j.version>
<!-- fastjson -->
<fastjson.version>1.1.40</fastjson.version>
</properties>
<dependencies>
<!-- axis需要的jar -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>${axis.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>${axis.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>${axis.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>${commons-discovery.version}</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>${wsdl4j.version}</version>
</dependency>
<!-- // axis需要的jar -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
Webservice工具类
package com.example.common.util.webservice.axis;
import org.apache.axis.Constants;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* <pre>
* WebService工具类
* </pre>
*
* <pre>
* @author mazq
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/12/17 14:07 修改内容:
* </pre>
*/
public class WebServiceClientUtils {
private static final Logger log = LoggerFactory.getLogger(WebServiceClientUtils.class);
private static final Integer CONNET_TIME_OUT = 8000;
public static String call(String point, String nameSpace, String methodName, Map<String, String> paramMap)
throws Exception {
// 创建call实例
Call call = null;
try {
call = (Call) new Service().createCall();
} catch (Throwable e) {
log.error("new call失败",e);
}
call.setTargetEndpointAddress(point);
call.setOperationName(new QName(nameSpace, methodName));
// 调用超时时间
call.setTimeout(CONNET_TIME_OUT);
// 存放入参数
List<Object> paramValues = new ArrayList<Object>();
if (paramMap != null && paramMap.size() > 0) {
for (Map.Entry<String, String> param : paramMap.entrySet()) {
call.addParameter(param.getKey(), Constants.XSD_STRING, ParameterMode.IN);
paramValues.add(param.getValue());
if (log.isInfoEnabled()) {
log.info("webService参数封装,参数:{},值为{}" , param.getKey(), param.getValue());
}
}
}
// 设置返回参数类型
call.setReturnType(Constants.XSD_STRING);
// 调用WebService服务
if (log.isInfoEnabled()) {
log.info("开始调用webService");
}
long start = System.currentTimeMillis();
Object object = call.invoke(paramValues.toArray());
long end = System.currentTimeMillis();
if (log.isInfoEnabled()) {
log.info("调用webService ;耗时:{}", (end - start) + "ms");
}
// WebService参数返回
String result = object.toString();
if (log.isInfoEnabled()) {
log.info("WebService参数返回:{}", result);
}
return result;
}
}
调用参数
// webservice请求参数
long timestamp = System.currentTimeMillis();
String md5Key = DigestUtils.md5DigestAsHex((username + password + timestamp).getBytes());
String paramJson="{"
+ "\"md5key\":\""+md5Key+"\","
+ "\"username\":\""+username+"\","
+ "\"timestamp\":\""+timestamp+"\""
+ "}";
if (logger.isInfoEnabled()) {
logger.info(String.format("参数打印>> username:%s,password:%s,timestamp:%s,md5Key:%s",
username, password, timestamp, md5Key));
logger.info(String.format("json参数:%s", paramJson));
}
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("in0", paramJson);
// webservice调用
String wsToken = WebServiceClinetUtil.call(point, null,"getSafeToken", paramMap);
if (logger.isInfoEnabled()) {
logger.info(String.format("webservice server返回json:%s", wsToken));
}
JSONObject jsonObject = JSONObject.fromObject(wsToken);
Map<String, Object> wsMap = JsonUtil.formatListMap(jsonObject);
final String code = wsMap.get("code").toString();
String token = "";
if ("200".equals(code)) {
token = wsMap.get("token").toString();
if (logger.isInfoEnabled()) {
logger.info(String.format("api token:%s", token));
}
} else {
logger.warn(String.format("api参数校验异常!code:%s,message:%s",wsMap.get("code").toString(),
wsMap.get("message").toString()));
}