SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例
前言
調(diào)用WebService接口的方式有很多,今天記錄一下,使用 Spring Web Services 調(diào)用 SOAP WebService接口
一.導(dǎo)入依賴
<!-- Spring Boot Web依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Web Services -->
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
</dependency>
<!-- Apache HttpClient 作為 WebService 客戶端 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- JAXB API -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<!-- JAXB 運(yùn)行時(shí) -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.5</version>
</dependency>
二.創(chuàng)建請(qǐng)求類和響應(yīng)類
根據(jù)SOAP的示例,創(chuàng)建請(qǐng)求類和響應(yīng)類
SOAP示例
請(qǐng)求
POST *****************
Host: **************
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://*******/DownloadFileByMaterialCode"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<DownloadFileByMaterialCode xmlns="http://*******/">
<MaterialCode>string</MaterialCode>
<FileType>string</FileType>
<Category>string</Category>
</DownloadFileByMaterialCode>
</soap:Body>
</soap:Envelope>
響應(yīng)
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<DownloadFileByMaterialCodeResponse xmlns="http://********/">
<DownloadFileByMaterialCodeResult>string</DownloadFileByMaterialCodeResult>
</DownloadFileByMaterialCodeResponse>
</soap:Body>
</soap:Envelope>
根據(jù)我的這個(gè)示例,我創(chuàng)建的請(qǐng)求類和響應(yīng)類,是這樣的
請(qǐng)求類
@Data
@XmlRootElement(name = "DownloadFileByMaterialCode", namespace = "http://*******/")
@XmlAccessorType(XmlAccessType.FIELD)
public class DownloadFileByMaterialCodeRequest {
@XmlElement(name = "MaterialCode", namespace = "http://*******/")
private String MaterialCode;
@XmlElement(name = "FileType", namespace = "http://*******/")
private String FileType;
@XmlElement(name = "Category", namespace = "http://*******/")
private String Category;
}
響應(yīng)類
@Data
@XmlRootElement(name = "DownloadFileByMaterialCodeResponse", namespace = "http://********/")
@XmlAccessorType(XmlAccessType.FIELD)
public class DownloadFileByMaterialCodeResponse {
@XmlElement(name = "DownloadFileByMaterialCodeResult", namespace = "http://********/")
private String DownloadFileByMaterialCodeResult;
}
三.創(chuàng)建ObjectFactory類
@XmlRegistry
public class ObjectFactory {
// 創(chuàng)建 DownloadFileByMaterialCodeRequest 的實(shí)例
public DownloadFileByMaterialCodeRequest createDownloadFileByMaterialCodeRequest() {
return new DownloadFileByMaterialCodeRequest();
}
// 創(chuàng)建 DownloadFileByMaterialCodeResponse 的實(shí)例
public DownloadFileByMaterialCodeResponse createDownloadFileByMaterialCodeResponse() {
return new DownloadFileByMaterialCodeResponse();
}
}
四.配置WebServiceTemplate
@Configuration
public class WebServiceConfig {
@Bean
public WebServiceTemplate webServiceTemplate() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("org.jeecg.modules.webservice"); // 包路徑,包含請(qǐng)求和響應(yīng)對(duì)象類
WebServiceTemplate template = new WebServiceTemplate(marshaller);
return template;
}
}
五.調(diào)用WebService接口
@Service
public class DownloadFileService {
@Autowired
private WebServiceTemplate webServiceTemplate;
public List<ResponseJsonObject> downloadFile(String materialCode, String fileType, String category) throws JsonProcessingException {
String uri = "http://192.168.***.***/DYDServiceTest/PlmService.asmx"; // WebService 的 URL
// 創(chuàng)建請(qǐng)求對(duì)象并設(shè)置參數(shù)
DownloadFileByMaterialCodeRequest request = new DownloadFileByMaterialCodeRequest();
request.setMaterialCode(materialCode);
request.setFileType(fileType);
request.setCategory(category);
// 設(shè)置 SOAPAction
String soapAction = "http://********/DownloadFileByMaterialCode"; // Web 服務(wù)指定的 SOAPAction
// 使用 SoapActionCallback 來(lái)設(shè)置 SOAPAction 頭
SoapActionCallback soapActionCallback = new SoapActionCallback(soapAction);
// 發(fā)送 SOAP 請(qǐng)求并獲取響應(yīng)
DownloadFileByMaterialCodeResponse response = (DownloadFileByMaterialCodeResponse)
webServiceTemplate.marshalSendAndReceive(uri, request, soapActionCallback);
// 獲取并返回 DownloadFileByMaterialCodeResult
String downloadFileByMaterialCodeResult = response.getDownloadFileByMaterialCodeResult();
System.out.println(downloadFileByMaterialCodeResult);
//字符串轉(zhuǎn)換為ResponseJsonObject對(duì)象
ObjectMapper objectMapper = new ObjectMapper();
// 忽略未知字段
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<ResponseJsonObject> ResponseJsonObjects = objectMapper.readValue(downloadFileByMaterialCodeResult, objectMapper.getTypeFactory().constructCollectionType(List.class, ResponseJsonObject.class));
return ResponseJsonObjects;
}
}
六.測(cè)試代碼
@Test
public void test1() throws JsonProcessingException {
List<ResponseJsonObject> responseJsonObjects = downloadFileService.downloadFile("CCPT0016-QBY-7", "", "");
for (ResponseJsonObject responseJsonObject : responseJsonObjects) {
System.out.println(responseJsonObject.getDocName());
}
}
測(cè)試效果

這里在附上所有文件的路勁圖,可以參考一下

總結(jié)
根據(jù)接口給出的SAOP的示例,封裝好對(duì)應(yīng)的實(shí)體類,因?yàn)槲疫@里的類型都是String,大家也可以根據(jù)實(shí)際情況,封裝好對(duì)應(yīng)的類
注意注解的參數(shù),namespace = “http://*******/” 給接口提供的域名地址
到此這篇關(guān)于SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot調(diào)用WebService接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot測(cè)試WebMVC的4種實(shí)現(xiàn)方案
在項(xiàng)目開發(fā)中,測(cè)試是確保應(yīng)用質(zhì)量的關(guān)鍵環(huán)節(jié),對(duì)于基于SpringBoot構(gòu)建的Web應(yīng)用,高效測(cè)試MVC層可以極大提高開發(fā)及聯(lián)調(diào)效率,下面我們來(lái)看看SpringBoot中測(cè)試WebMVC的4種實(shí)現(xiàn)方案吧2025-04-04
idea復(fù)制module(項(xiàng)目)并在一個(gè)窗口展示的教程詳解
這篇文章主要介紹了idea復(fù)制module(項(xiàng)目)并在一個(gè)窗口展示的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
MyBatis之關(guān)于動(dòng)態(tài)SQL解讀
這篇文章主要介紹了MyBatis之關(guān)于動(dòng)態(tài)SQL解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
SpringBoot中項(xiàng)目如何讀取外置logback配置文件
這篇文章主要介紹了SpringBoot中項(xiàng)目如何讀取外置logback配置文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
java發(fā)送飛書消息卡片具體實(shí)現(xiàn)方法
這篇文章主要介紹了java發(fā)送飛書消息卡片具體實(shí)現(xiàn)方法的相關(guān)資料,Java通過(guò)飛書消息卡片API,可以高效解決傳統(tǒng)消息推送方式的痛點(diǎn),實(shí)現(xiàn)業(yè)務(wù)系統(tǒng)事件與飛書消息卡片之間的全流程閉環(huán),需要的朋友可以參考下2026-03-03
mybatis中mapper代理的生成過(guò)程全面分析
這篇文章主要為大家介紹了mybatis中mapper代理的生成過(guò)程全面分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

