SpringBoot調(diào)用WebService接口方法示例代碼
前言
調(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 運行時 -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.5</version>
</dependency>二.創(chuàng)建請求類和響應(yīng)類
根據(jù)SOAP的示例,創(chuàng)建請求類和響應(yīng)類
SOAP示例
請求
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ù)我的這個示例,我創(chuàng)建的請求類和響應(yī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 的實例
public DownloadFileByMaterialCodeRequest createDownloadFileByMaterialCodeRequest() {
return new DownloadFileByMaterialCodeRequest();
}
// 創(chuàng)建 DownloadFileByMaterialCodeResponse 的實例
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"); // 包路徑,包含請求和響應(yīng)對象類
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)建請求對象并設(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 請求并獲取響應(yīng)
DownloadFileByMaterialCodeResponse response = (DownloadFileByMaterialCodeResponse)
webServiceTemplate.marshalSendAndReceive(uri, request, soapActionCallback);
// 獲取并返回 DownloadFileByMaterialCodeResult
String downloadFileByMaterialCodeResult = response.getDownloadFileByMaterialCodeResult();
System.out.println(downloadFileByMaterialCodeResult);
//字符串轉(zhuǎn)換為ResponseJsonObject對象
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;
}
}六.測試代碼
@Test
public void test1() throws JsonProcessingException {
List<ResponseJsonObject> responseJsonObjects = downloadFileService.downloadFile("CCPT0016-QBY-7", "", "");
for (ResponseJsonObject responseJsonObject : responseJsonObjects) {
System.out.println(responseJsonObject.getDocName());
}
}測試效果

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

總結(jié)
根據(jù)接口給出的SAOP的示例,封裝好對應(yīng)的實體類,因為我這里的類型都是String,大家也可以根據(jù)實際情況,封裝好對應(yīng)的類
注意注解的參數(shù),namespace = "http://*******/" 給接口提供的域名地址
到此這篇關(guān)于SpringBoot調(diào)用WebService接口的文章就介紹到這了,更多相關(guān)SpringBoot調(diào)用WebService接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot整合cxf發(fā)布webservice以及調(diào)用的方法
- SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類型)
- SpringBoot項目使用?axis?調(diào)用webservice接口的實踐記錄
- webservice實現(xiàn)springboot項目間接口調(diào)用與對象傳遞示例
- SpringBoot調(diào)用第三方WebService接口的兩種方法
- SpringBoot調(diào)用對方webService接口的幾種方法示例
- springboot調(diào)用webservice-soap接口的實現(xiàn)
- springboot使用webservice發(fā)布和調(diào)用接口的實例詳解
相關(guān)文章
System.getProperty(user.dir)定位問題解析
System.getProperty(user.dir) 獲取的是啟動項目的容器位置,用IDEA是項目的根目錄,部署在tomcat上是tomcat的啟動路徑,即tomcat/bin的位置,這篇文章主要介紹了System.getProperty(user.dir)定位問題,需要的朋友可以參考下2023-05-05
解決spirngboot連接redis報錯:READONLY?You?can‘t?write?against?
docker部署的redis,springboot基本每天來連redis都報錯:READONLY?You?can't?write?against?a?read?only?replica,重啟redis后,可以正常連接。但是每天都重啟redis,不現(xiàn)實,也很麻煩,今天給大家分享解決方式,感興趣的朋友一起看看吧2023-06-06
SpringBoot整合ZXing實現(xiàn)二維碼和條形碼的創(chuàng)建
如今我們越來越多的東西需要用到二維碼或者條形碼,商品的條形碼,付款的二維碼等等,所以本文小編給大家介紹了SpringBoot整合ZXing實現(xiàn)二維碼和條形碼的創(chuàng)建,文章通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2023-12-12
java實現(xiàn)遍歷樹形菜單兩種實現(xiàn)代碼分享
這篇文章主要介紹了java實現(xiàn)遍歷樹形菜單兩種實現(xiàn)代碼分享,兩種實現(xiàn):OpenSessionView實現(xiàn)、TreeAction實現(xiàn)。具有一定參考價值,需要的朋友可以了解下。2017-11-11
SpringBoot如何在運行時動態(tài)添加數(shù)據(jù)源
這篇文章主要介紹了SpringBoot如何在運行時動態(tài)添加數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
java HashMap和HashTable的區(qū)別詳解
這篇文章主要介紹了java HashMap和HashTable的區(qū)別詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12
解決@Autowired注入空指針問題(利用Bean的生命周期)
這篇文章主要介紹了解決@Autowired注入空指針問題(利用Bean的生命周期),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

