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

网友投稿 2441 2022-12-14

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

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

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是-开发提供的webservice服务。

依赖

引入相关依赖:

org.springframework.boot

spring-boot-starter-web-services

org.apache.cxf

cxf-spring-boot-starter-jaxws

3.2.1

org.apache.cxf

cxf-rt-transports-http

3.2.1

浏览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.newIhttp://nstance();

//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("", "").replace("", "");

log.info("ticket request params xml: {}", requestXml);

/**

* 调用webservice请求

*/

HttpHeaders headers = new HttpHeaders();

//header参数

headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

//请求参数

MultiValueMap map = new LinkedMultiValueMap<>();

http:// //接口参数

map.add("msg", requestXml);

//构造实体对象

HttpEntity> 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 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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring 实现给Bean属性注入null值
下一篇:springboot清除字符串前后空格与防xss攻击方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~