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 來設(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)文章
通過JDBC連接oracle數(shù)據(jù)庫的十大技巧
通過JDBC連接oracle數(shù)據(jù)庫的十大技巧...2006-12-12
Java并發(fā)編程示例(七):守護(hù)線程的創(chuàng)建和運(yùn)行
這篇文章主要介紹了Java并發(fā)編程示例(七):守護(hù)線程的創(chuàng)建和運(yùn)行,在本節(jié)示例中,我們將創(chuàng)建兩個(gè)線程,一個(gè)是普通線程,向隊(duì)列中寫入事件,另外一個(gè)是守護(hù)線程,清除隊(duì)列中的事件,需要的朋友可以參考下2014-12-12
Java中ArrayList和LinkedList區(qū)別
這篇文章主要介紹了Java中ArrayList和LinkedList區(qū)別,下面我們就重點(diǎn)聊一聊在日常開發(fā)中經(jīng)常被使用到的兩個(gè)集合類ArrayList和LinkedList的本質(zhì)區(qū)別吧,需要的朋友可以參考一下2022-01-01
Java?HttpURLConnection使用方法與實(shí)例演示分析
這篇文章主要介紹了Java?HttpURLConnection使用方法與實(shí)例演示,HttpURLConnection一個(gè)抽象類是標(biāo)準(zhǔn)的JAVA接口,該類位于java.net包中,它提供了基本的URL請(qǐng)求,響應(yīng)等功能,下面我們來深入看看2023-10-10
使用JWT創(chuàng)建解析令牌及RSA非對(duì)稱加密詳解
這篇文章主要介紹了JWT創(chuàng)建解析令牌及RSA非對(duì)稱加密詳解,JWT是JSON Web Token的縮寫,即JSON Web令牌,是一種自包含令牌,一種情況是webapi,類似之前的阿里云播放憑證的功能,另一種情況是多web服務(wù)器下實(shí)現(xiàn)無狀態(tài)分布式身份驗(yàn)證,需要的朋友可以參考下2023-11-11
Spring?Security+JWT如何實(shí)現(xiàn)前后端分離權(quán)限控制
本篇將手把手教你用?Spring?Security?+?JWT?搭建一套完整的登錄認(rèn)證與權(quán)限控制體系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
javaSE基礎(chǔ)如何通俗的理解javaBean是什么
所謂的Java Bean,就是一個(gè)java類,編譯后成為了一個(gè)后綴名是 .class的文件。這就是Java Bean,很多初學(xué)者,包括當(dāng)年的我自己,總是被這些專有名詞搞的暈頭轉(zhuǎn)向2021-10-10

