WebService系列之使用Axis调用第三方wsdl接口
【摘要】 WebService系列之使用Axis调用第三方wsdl接口
测试工具下载soapui测试
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
pom配置:
<properties> <!-- axis --> <axis.version>1.4</axis.version> <!-- common...
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
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; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
调用参数
// 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()));
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
文章来源: smilenicky.blog.csdn.net,作者:smileNicky,版权归原作者所有,如需转载,请联系作者。
原文链接:smilenicky.blog.csdn.net/article/details/111059777
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)