SpringBoot调⽤第三⽅WebService接⼝的操作技巧(.wsdl与.asmx类型)依赖
引⼊相关依赖:
<!-- webService-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- CXF webservice -->
<dependency>
<groupId>f</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>f</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.1</version>
</dependency>
浏览webService提供的⽅法,确定⼊参顺序直接在浏览器⾥⾯访问url,如下
⽤SoapUI⼯具
⽤些是.asmx格式,也可以直接在浏览器访问。会列出⽅法列表
代码
创建client:
package com.figuration;
import com.stant.CommonConstant;
slf4j.Slf4j;
import f.endpoint.Client;
import dpoint.dynamic.JaxWsDynamicClientFactory; import f.transport.http.HTTPConduit;
import f.figuration.HTTPClientPolicy;
import t.annotation.Bean;
import t.annotation.Configuration;
/**
* @author gqzdev
* @date 2021/08/26 15:53
**/
@Configuration
@Slf4j
public class JaxWsClientConfig {
@Bean("JaxWsClient")
public Client client() {
// 创建动态客户端
JaxWsDynamicClientFactory clientFactory = wInstance();
//CommonConstant.PUBLIC_SECURITY_URL为连接的url,如10.189.200.170:9201/wharfWebService/services/WharfService?wsdl log.info("publicsecurity webService url : {}", CommonConstant.PUBLIC_SECURITY_URL);
//创建client
Client client = ateClient(CommonConstant.PUBLIC_SECURITY_URL);
HTTPConduit conduit = (HTTPConduit) Conduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setAllowChunking(false);
// 连接服务器超时时间 10秒
policy.setConnectionTimeout(10000);
// 等待服务器响应超时时间 20秒
policy.setReceiveTimeout(20000);
调用webservice服务conduit.setClient(policy);
return client;
}
}
有了client,便可以直接注⼊调⽤invoke。到⾃⼰需要调⽤的⽅法:
下⾯只展⽰⼀个⽅法的调⽤演⽰,其他的类似
package com.gqzdev.sctads.service.impl;
import com.stant.CommonConstant;
import com.gqzdev.sctads.service.PublicSecurityService;
slf4j.Slf4j;
import f.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;
l.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 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();
<("invoke WS userAddNew method error: {}", e.getMessage());
}
return null;
}
}
使⽤post请求访问webservice接⼝
package com.gqzdev.sctads.service.impl;
import util.XmlUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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 = JavaObject(JSONObject.JSONString(ticketRequestVO)), Map.class);
String objXml = XmlUtil.mapToXmlStr(objMap);
String requestXml = place("<xml>", "<MQ_MESSAGE>").replace("</xml>", "</MQ_MESSA
GE>");
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 = lToMap(responseXml);
TicketResponseVO ticketResponseVO = JsonUtil.map2pojo(resultMap, TicketResponseVO.class);
//0开闸,1不开闸
if (MQ_MESSAGE().getMSG_BODY().getCOMMAND() == 0) {
return true;
}
return false;
}
}
XML相关操作
关于xml解析处理,这边推荐使⽤hutool⾥⾯的XmlUtil
到此这篇关于SpringBoot调⽤第三⽅WebService接⼝(.wsdl与.asmx类型)的⽂章就介绍到这了,更
多相关SpringBoot WebService接⼝内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论