WebService系列之使用Axis获取天气预报数据
【摘要】 测试工具下载soapui测试
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
pom配置:
<properties> <!-- axis --> <axis.version>1.4</axis.version> <!-- commons-iscovery--> <commons-discovery...
测试工具下载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
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.apache.axis.encoding.XMLType;
import org.apache.axis.types.Schema;
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.HashMap;
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 void call(String point, String nameSpace, String methodName, Map<String, String> paramMap) throws Exception { // 创建call实例 log.info("wsdl链接:{},命名空间:{},方法名:{}" , point , nameSpace , methodName); 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.setUseSOAPAction(true); call.setSOAPActionURI(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(new QName(nameSpace, 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); //call.setReturnType(XMLType.SOAP_DOCUMENT); //call.setReturnType(XMLType.XSD_SCHEMA); call.setReturnClass(java.lang.String[].class); // 调用WebService服务 if (log.isInfoEnabled()) { log.info("开始调用webService"); } long start = System.currentTimeMillis(); String[] res = (String[]) call.invoke(paramValues.toArray()); long end = System.currentTimeMillis(); if (log.isInfoEnabled()) { log.info("调用webService ;耗时:{}", (end - start) + "ms"); } // WebService参数返回 //String result = object.toString(); for (int i = 0; i < res.length; i++) { System.out.println(res[i]); if (log.isInfoEnabled()) { log.info("WebService参数返回:{}", res[i]); } } } public static void main(String[] args) throws Exception { String point = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl"; String nameSpace = "http://WebXml.com.cn/"; String methodName = "getWeather"; String theCityCode = "北京"; String theUserID = ""; Map<String, String> paramMap = new HashMap<String, String>(2); paramMap.put("theCityCode", theCityCode); paramMap.put("theUserID", theUserID); call(point, nameSpace, methodName, paramMap); }
}
- 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
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
文章来源: smilenicky.blog.csdn.net,作者:smileNicky,版权归原作者所有,如需转载,请联系作者。
原文链接:smilenicky.blog.csdn.net/article/details/111321504
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)