SpringBoot調(diào)用WebService接口方法示例代碼
前言
調(diào)用WebService接口的方式有很多,今天記錄一下,使用 Spring Web Services 調(diào)用 SOAP WebService接口
一.導(dǎo)入依賴(lài)
<!-- Spring Boot Web依賴(lài) -->
<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)求類(lèi)和響應(yīng)類(lèi)
根據(jù)SOAP的示例,創(chuàng)建請(qǐng)求類(lèi)和響應(yīng)類(lèi)
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)求類(lèi)和響應(yīng)類(lèi),是這樣的
請(qǐng)求類(lèi)
@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)類(lèi)
@Data
@XmlRootElement(name = "DownloadFileByMaterialCodeResponse", namespace = "http://********/")
@XmlAccessorType(XmlAccessType.FIELD)
public class DownloadFileByMaterialCodeResponse {
@XmlElement(name = "DownloadFileByMaterialCodeResult", namespace = "http://********/")
private String DownloadFileByMaterialCodeResult;
}三.創(chuàng)建ObjectFactory類(lèi)
@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ì)象類(lèi)
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í)體類(lèi),因?yàn)槲疫@里的類(lèi)型都是String,大家也可以根據(jù)實(shí)際情況,封裝好對(duì)應(yīng)的類(lèi)
注意注解的參數(shù),namespace = "http://*******/" 給接口提供的域名地址
到此這篇關(guān)于SpringBoot調(diào)用WebService接口的文章就介紹到這了,更多相關(guān)SpringBoot調(diào)用WebService接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot整合cxf發(fā)布webservice以及調(diào)用的方法
- SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類(lèi)型)
- SpringBoot項(xiàng)目使用?axis?調(diào)用webservice接口的實(shí)踐記錄
- webservice實(shí)現(xiàn)springboot項(xiàng)目間接口調(diào)用與對(duì)象傳遞示例
- SpringBoot調(diào)用第三方WebService接口的兩種方法
- SpringBoot調(diào)用對(duì)方webService接口的幾種方法示例
- springboot調(diào)用webservice-soap接口的實(shí)現(xiàn)
- springboot使用webservice發(fā)布和調(diào)用接口的實(shí)例詳解
相關(guān)文章
使用Spring自定義實(shí)現(xiàn)IOC和依賴(lài)注入(注解方式)
這篇文章主要介紹了使用Spring自定義實(shí)現(xiàn)IOC和依賴(lài)注入(注解方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
JavaWeb 中Cookie實(shí)現(xiàn)記住密碼的功能示例
cookie是一種WEB服務(wù)器通過(guò)瀏覽器在訪問(wèn)者的硬盤(pán)上存儲(chǔ)信息的手段。Cookie的目的就是為用戶帶來(lái)方便,為網(wǎng)站帶來(lái)增值。這篇文章主要介紹了JavaWeb 中Cookie實(shí)現(xiàn)記住密碼的功能示例,需要的朋友可以參考下2017-06-06
Java中replace與replaceAll的區(qū)別與測(cè)試
replace和replaceAll是JAVA中常用的替換字符的方法,下面這篇文章主要給大家介紹了關(guān)于Java中replace與replaceAll的區(qū)別與測(cè)試,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
java GUI實(shí)現(xiàn)學(xué)生圖書(shū)管理簡(jiǎn)單實(shí)例
這篇文章主要為大家詳細(xì)介紹了java GUI實(shí)現(xiàn)學(xué)生圖書(shū)管理簡(jiǎn)單示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
SpringAOP 如何通過(guò)JoinPoint獲取參數(shù)名和值
這篇文章主要介紹了SpringAOP 通過(guò)JoinPoint獲取參數(shù)名和值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

