SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類型)
SpringBoot調(diào)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開發(fā)提供的webservice服務(wù)。
依賴
引入相關(guān)依賴:
<!-- 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提供的方法,確定入?yún)㈨樞?直接在瀏覽器里面訪問url,如下

用SoapUI工具

用些是.asmx格式,也可以直接在瀏覽器訪問。會列出方法列表

代碼
創(chuàng)建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() {
// 創(chuàng)建動態(tài)客戶端
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);
//創(chuàng)建client
Client client = clientFactory.createClient(CommonConstant.PUBLIC_SECURITY_URL);
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setAllowChunking(false);
// 連接服務(wù)器超時時間 10秒
policy.setConnectionTimeout(10000);
// 等待服務(wù)器響應(yīng)超時時間 20秒
policy.setReceiveTimeout(20000);
conduit.setClient(policy);
return client;
}
}
有了client,便可以直接注入調(diào)用invoke。找到自己需要調(diào)用的方法:
下面只展示一個方法的調(diào)用演示,其他的類似
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;
/**
* 對接票務(wù)系統(tǒng)驗證
*
* @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) {
//構(gòu)造請求參數(shù)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);
/**
* 調(diào)用webservice請求
*/
HttpHeaders headers = new HttpHeaders();
//header參數(shù)
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//請求參數(shù)
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
//接口參數(shù)
map.add("msg", requestXml);
//構(gòu)造實體對象
HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
//發(fā)送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相關(guān)操作
關(guān)于xml解析處理,這邊推薦使用hutool里面的XmlUtil
到此這篇關(guān)于SpringBoot調(diào)用第三方WebService接口(.wsdl與.asmx類型 )的文章就介紹到這了,更多相關(guān)SpringBoot WebService接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)動態(tài)數(shù)據(jù)源切換的實踐指南
在 Java 開發(fā)中,許多場景需要訪問多個數(shù)據(jù)庫,例如多租戶系統(tǒng)或讀寫分離架構(gòu),為了靈活高效地管理這些場景,動態(tài)數(shù)據(jù)源切換技術(shù)應(yīng)運而生,所以本文給大家介紹了Java實現(xiàn)動態(tài)數(shù)據(jù)源切換的實踐指南,需要的朋友可以參考下2025-03-03
mybatis-flex實現(xiàn)多數(shù)據(jù)源操作
MyBaits-Flex內(nèi)置了功能完善的多數(shù)據(jù)源支持,本文主要介紹了mybatis-flex實現(xiàn)多數(shù)據(jù)源操作,具有一定的參考價值,感興趣的可以了解一下2024-06-06
Spring?AI借助全局參數(shù)實現(xiàn)智能數(shù)據(jù)庫操作與個性化待辦管理
這篇文章主要介紹了Spring?AI借助全局參數(shù)實現(xiàn)智能數(shù)據(jù)庫操作與個性化待辦管理,本文給大家介紹的非常詳細,需要的朋友可以參考下2024-11-11
java web在高并發(fā)和分布式下實現(xiàn)訂單號生成唯一的解決方案
這篇文章主要介紹了java web在高并發(fā)和分布式下實現(xiàn)訂單號生成唯一的解決方案,需要的朋友可以參考下2017-11-11
Java的Socket實現(xiàn)長連接以及數(shù)據(jù)的發(fā)送和接收方式
這篇文章主要介紹了Java的Socket實現(xiàn)長連接以及數(shù)據(jù)的發(fā)送和接收方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Springboot jar文件如何打包zip在linux環(huán)境運行
這篇文章主要介紹了Springboot jar文件如何打包zip在linux環(huán)境運行,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2020-02-02
Spring Boot整合QueryDSL的實現(xiàn)示例
這篇文章主要介紹了Spring Boot整合QueryDSL的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-09-09
mybatis-plus開啟sql打印的三種方式總結(jié)
這篇文章主要給大家介紹了mybatisplus開啟sql打印的三種方式,文章通過代碼示例介紹的非常詳細,對大家的學(xué)習或工作有一定的參考價值,需要的朋友可以參考下2023-11-11
Java實現(xiàn)將PDF轉(zhuǎn)為PDF/A
通過將PDF格式轉(zhuǎn)換為PDF/A格式,可保護文檔布局、格式、字體、大小等不受更改,從而實現(xiàn)文檔安全保護的目的,同時又能保證文檔可讀、可訪問。本文將為大家介紹如何實現(xiàn)這一轉(zhuǎn)換,需要的可以參考一下2022-01-01

