前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WebService系列之使用Axis调用第三方wsdl接口

WebService系列之使用Axis调用第三方wsdl接口

作者头像
SmileNicky
发布2020-12-21 10:27:04
1.5K0
发布2020-12-21 10:27:04
举报
文章被收录于专栏:Nicky's blog

WebService系列之使用Axis调用第三方wsdl接口

测试工具下载soapui测试

http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

pom配置:

代码语言:javascript
复制
<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工具类

代码语言:javascript
复制
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;
    }

}

调用参数

代码语言:javascript
复制
// 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()));
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/12/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档