SpringBoot調(diào)用WebService接口方法示例代碼
前言
調(diào)用WebService接口的方式有很多,今天記錄一下,使用 Spring Web Services 調(diào)用 SOAP WebService接口
一.導入依賴
<!-- 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)建請求類和響應類
根據(jù)SOAP的示例,創(chuà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> 響應 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)建的請求類和響應類,是這樣的
請求類
@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; }
響應類
@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"); // 包路徑,包含請求和響應對象類 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ù) DownloadFileByMaterialCodeRequest request = new DownloadFileByMaterialCodeRequest(); request.setMaterialCode(materialCode); request.setFileType(fileType); request.setCategory(category); // 設置 SOAPAction String soapAction = "http://********/DownloadFileByMaterialCode"; // Web 服務指定的 SOAPAction // 使用 SoapActionCallback 來設置 SOAPAction 頭 SoapActionCallback soapActionCallback = new SoapActionCallback(soapAction); // 發(fā)送 SOAP 請求并獲取響應 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()); } }
測試效果
這里在附上所有文件的路勁圖,可以參考一下
總結
根據(jù)接口給出的SAOP的示例,封裝好對應的實體類,因為我這里的類型都是String,大家也可以根據(jù)實際情況,封裝好對應的類
注意注解的參數(shù),namespace = "http://*******/" 給接口提供的域名地址
到此這篇關于SpringBoot調(diào)用WebService接口的文章就介紹到這了,更多相關SpringBoot調(diào)用WebService接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- 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)用接口的實例詳解
相關文章
使用Spring自定義實現(xiàn)IOC和依賴注入(注解方式)
這篇文章主要介紹了使用Spring自定義實現(xiàn)IOC和依賴注入(注解方式),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08JavaWeb 中Cookie實現(xiàn)記住密碼的功能示例
cookie是一種WEB服務器通過瀏覽器在訪問者的硬盤上存儲信息的手段。Cookie的目的就是為用戶帶來方便,為網(wǎng)站帶來增值。這篇文章主要介紹了JavaWeb 中Cookie實現(xiàn)記住密碼的功能示例,需要的朋友可以參考下2017-06-06Java中replace與replaceAll的區(qū)別與測試
replace和replaceAll是JAVA中常用的替換字符的方法,下面這篇文章主要給大家介紹了關于Java中replace與replaceAll的區(qū)別與測試,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09SpringAOP 如何通過JoinPoint獲取參數(shù)名和值
這篇文章主要介紹了SpringAOP 通過JoinPoint獲取參數(shù)名和值的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06