SpringBoot调用第三方WebService接口的操作技巧(.wsdl与.asmx类型)

 更新时间:2021年8月28日 00:00  点击:2091

SpringBoot调webservice接口,一般都会给你url如:
http://10.189.200.170:9201/wharfWebService/services/WharfService?wsdl
http://10.103.6.158:35555/check_ticket/Ticket_Check.asmx
其中.asmx是.net开发提供的webservice服务。

依赖

引入相关依赖:

<!-- webService-->
 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web-services</artifactId>
  </dependency>

  <!-- CXF webservice -->
  <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
      <version>3.2.1</version>
  </dependency>
  <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>3.2.1</version>
  </dependency>

浏览webService提供的方法,确定入参顺序 直接在浏览器里面访问url,如下

在这里插入图片描述

用SoapUI工具

在这里插入图片描述

用些是.asmx格式,也可以直接在浏览器访问。会列出方法列表

在这里插入图片描述

代码

创建client:

package com.gqzdev.sctads.configuration;

import com.gqzdev.sctads.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author gqzdev
 * @date 2021/08/26 15:53
 **/
@Configuration
@Slf4j
public class JaxWsClientConfig {

    @Bean("JaxWsClient")
    public Client client() {
        // 创建动态客户端
        JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
		//CommonConstant.PUBLIC_SECURITY_URL为连接的url,如http://10.189.200.170:9201/wharfWebService/services/WharfService?wsdl
        log.info("publicsecurity webService url : {}", CommonConstant.PUBLIC_SECURITY_URL);
        //创建client
        Client client = clientFactory.createClient(CommonConstant.PUBLIC_SECURITY_URL);
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setAllowChunking(false);
        // 连接服务器超时时间 10秒
        policy.setConnectionTimeout(10000);
        // 等待服务器响应超时时间 20秒
        policy.setReceiveTimeout(20000);
        conduit.setClient(policy);
        return client;
    }
}

有了client,便可以直接注入调用invoke。找到自己需要调用的方法:
下面只展示一个方法的调用演示,其他的类似

package com.gqzdev.sctads.service.impl;

import com.gqzdev.sctads.constant.CommonConstant;
import com.gqzdev.sctads.service.PublicSecurityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.xml.namespace.QName;

/**
 * @author gqzdev
 * @date 2021/08/26 15:24
 **/
@Slf4j
@Component
public class PublicSecurityServiceImpl implements PublicSecurityService {


    @Autowired
    @Qualifier("JaxWsClient")
    private Client client;

    /**
     * 旅客登记
     */
    @Override
//    @Async
    public String chineseAddNew(Object... params) {
//        QName qname = new QName("service.", CommonConstant.CHINESE_ADD);
        try {
            Object[] invoke = client.invoke(CommonConstant.CHINESE_ADD, params);
            if (invoke != null && invoke.length >= 1) {
                String result = (String) invoke[0];
                log.info("userAddNew result: {}", result);
                return result;
            }

        } catch (Exception e) {
//            e.printStackTrace();
            log.error("invoke WS userAddNew method error: {}", e.getMessage());
        }
        return null;
    }    
}

使用post请求访问webservice接口

package com.gqzdev.sctads.service.impl;

import cn.hutool.core.util.XmlUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 对接票务系统验证
 *
 * @author gqzdev
 * @date 2021/08/25 17:07
 **/
@Component
@Slf4j
public class TicketCheckServiceImpl implements TicketCheckService {

    @Autowired
    @Qualifier("nativeRestTemplate")
    private RestTemplate restTemplate;

    @Override
    public boolean ticketCheck(TicketRequestVO ticketRequestVO) {

        //构造请求参数xml
        Map objMap = JSONObject.toJavaObject(JSONObject.parseObject(JSON.toJSONString(ticketRequestVO)), Map.class);
        String objXml = XmlUtil.mapToXmlStr(objMap);
        String requestXml = objXml.replace("<xml>", "<MQ_MESSAGE>").replace("</xml>", "</MQ_MESSAGE>");
        log.info("ticket request params xml: {}", requestXml);

        /**
         * 调用webservice请求
         */
        HttpHeaders headers = new HttpHeaders();
        //header参数
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //请求参数
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        //接口参数
        map.add("msg", requestXml);
        //构造实体对象
        HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
        //发送post请求webservice
        String response = restTemplate.postForObject(CommonConstant.TICKET_REQUEST_URL, param, String.class);
        log.info("ticket request response: {}", response);

        /**
         * 解析返回xml,返回是否认证成功
         */
        String responseXml = XmlUtil.unescape(response);
        Map<String, Object> resultMap = XmlUtil.xmlToMap(responseXml);
        TicketResponseVO ticketResponseVO = JsonUtil.map2pojo(resultMap, TicketResponseVO.class);
        //0开闸 ,1不开闸
        if (ticketResponseVO.getMQ_MESSAGE().getMSG_BODY().getCOMMAND() == 0) {
            return true;
        }
        return false;
    }


}

XML相关操作

关于xml解析处理,这边推荐使用hutool里面的XmlUtil

到此这篇关于SpringBoot调用第三方WebService接口(.wsdl与.asmx类型 )的文章就介绍到这了,更多相关SpringBoot WebService接口内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

[!--infotagslink--]

相关文章

  • 解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题

    这篇文章主要介绍了解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-28
  • c# 三种方法调用WebService接口

    这篇文章主要介绍了c# 三种方法调用WebService接口的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-07
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • 详解springBoot启动时找不到或无法加载主类解决办法

    这篇文章主要介绍了详解springBoot启动时找不到或无法加载主类解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
  • vue接口请求加密实例

    这篇文章主要介绍了vue接口请求加密实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-12
  • SpringBoot集成Redis实现消息队列的方法

    这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
  • c#动态调用Webservice的两种方法实例

    这篇文章介绍了c#动态调用Webservice的两种方法实例,有需要的朋友可以参考一下...2020-06-25
  • 解决Springboot get请求是参数过长的情况

    这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
  • Spring Boot项目@RestController使用重定向redirect方式

    这篇文章主要介绍了Spring Boot项目@RestController使用重定向redirect方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-02
  • Springboot+TCP监听服务器搭建过程图解

    这篇文章主要介绍了Springboot+TCP监听服务器搭建过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-10-28
  • springBoot 项目排除数据库启动方式

    这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
  • c#中WebService的介绍及调用方式小结

    这篇文章主要给大家介绍了关于c#中的WebService及其调用方式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • C#简单了解接口(Interface)使用方法

    这篇文章主要介绍了C#简单了解接口(Interface)使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-12-08
  • 详解SpringBoot之访问静态资源(webapp...)

    这篇文章主要介绍了详解SpringBoot之访问静态资源(webapp...),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-14
  • SpringBoot接口接收json参数解析

    这篇文章主要介绍了SpringBoot接口接收json参数解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-19
  • springboot中使用@Transactional注解事物不生效的坑

    这篇文章主要介绍了springboot中使用@Transactional注解事物不生效的原因,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26
  • C# Rx的主要接口深入理解

    这篇文章主要介绍了C# Rx的主要接口深入理解的相关资料,需要的朋友可以参考下...2020-06-25
  • Feign接口方法返回值设置方式

    这篇文章主要介绍了Feign接口方法返回值设置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-07-08
  • springboot多模块包扫描问题的解决方法

    这篇文章主要介绍了springboot多模块包扫描问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
  • 如何设计一个安全的API接口详解

    在日常开发中,总会接触到各种接口,前后端数据传输接口,第三方业务平台接口,下面这篇文章主要给大家介绍了关于如何设计一个安全的API接口的相关资料,需要的朋友可以参考下...2021-08-12