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

    }
}

在这里插入图片描述

smileNicky CSDN认证博客专家 分布式 Java Spring
java程序员,软件工程专业,专注于sql调优,SpringBoot,spring框架学习,个人邮箱nickypm@foxmail.com,公众号nickymp
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页
实付 9.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值