Java(Springboot)項(xiàng)目調(diào)用第三方WebService接口實(shí)現(xiàn)代碼
WebService 簡介
WebService 接口的發(fā)布通常一般都是使用 WSDL(web service descriptive language)文件的樣式來發(fā)布的,該文檔包含了請求的參數(shù)信息,返回的結(jié)果信息,我們需要根據(jù) WSDL 文檔的信息來編寫相關(guān)的代碼進(jìn)行調(diào)用WebService接口。
業(yè)務(wù)場景描述
目前需要使用 java 調(diào)用一個WebService接口,傳遞參數(shù)的數(shù)據(jù)類型為 xml,返回的也是一個Xml的數(shù)據(jù)類型,需要實(shí)現(xiàn)調(diào)用接口,獲取到xml 之后并解析為 Json 格式數(shù)據(jù),并返回所需結(jié)果給前端。
WSDL 文檔
請求地址及方式
接口地址 | 請求方式 |
---|---|
http://aabb.balabala.com.cn/services/BD?wsdl | SOAP |
接口請求/響應(yīng)報文
<?xml version="1.0" encoding="UTF-8"?> <TransData> <BaseInfo> <PrjID>BBB</PrjID> <UserID>UID</UserID> </BaseInfo> <InputData> <WriteType>220330</WriteType> <HandCode>8</HandCode> </InputData> <OutputData> <ResultCode>0</ResultCode> <ResultMsg>獲取權(quán)限編號成功!</ResultMsg> <OrgniseNo>SHUG98456</OrgniseNo> </OutputData> </TransData>
代碼實(shí)現(xiàn)
1、接口請求/響應(yīng)報文 JSON 準(zhǔn)備
首先準(zhǔn)備好所需要的請求參數(shù)和返回數(shù)據(jù)的實(shí)體類
(1)TransData
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "TransData") @XmlAccessorType(XmlAccessType.FIELD) public class TransDataDto { @XmlElement(name = "BaseInfo") private BaseInfoDto baseInfo; @XmlElement(name = "InputData") private InputDataDto inputData; @XmlElement(name = "OutputData") private OutputDataDto outputData; public BaseInfoDto getBaseInfo() { return baseInfo; } public void setBaseInfo(BaseInfoDto baseInfo) { this.baseInfo = baseInfo; } public InputDataDto getInputData() { return inputData; } public void setInputData(InputDataDto inputData) { this.inputData = inputData; } public OutputDataDto getOutputData() { return outputData; } public void setOutputData(OutputDataDto outputData) { this.outputData = outputData; } }
(2)BaseInfo、InputData、OutputData
BaseInfo
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "BaseInfo") @XmlAccessorType(XmlAccessType.FIELD) public class BaseInfoDto { @XmlElement(name = "PrjID") private String prjId; @XmlElement(name = "UserID") private String userId; public String getPrjId() { return prjId; } public void setPrjId(String prjId) { this.prjId = prjId; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } }
InputData
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "InputData") @XmlAccessorType(XmlAccessType.FIELD) public class InputDataDto { @XmlElement(name = "WriteType") private String writeType; @XmlElement(name = "HandCode") private String handCode; public String getWriteType() { return writeType; } public void setWriteType(String writeType) { this.writeType = writeType; } public String getHandCode() { return handCode; } public void setHandCode(String handCode) { this.handCode = handCode; } }
OutputData
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "OutputData") @XmlAccessorType(XmlAccessType.FIELD) public class OutputDataDto { @XmlElement(name = "ResultCode") private String resultCode; @XmlElement(name = "ResultMsg") private String resultMsg; @XmlElement(name = "OrgniseNo") private String orgniseNo; public String getResultCode() { return resultCode; } public void setResultCode(String resultCode) { this.resultCode = resultCode; } public String getResultMsg() { return resultMsg; } public void setResultMsg(String resultMsg) { this.resultMsg = resultMsg; } public String getOrgniseNo() { return orgniseNo; } public void setOrgniseNo(String orgniseNo) { this.orgniseNo = orgniseNo; } }
2、業(yè)務(wù)邏輯實(shí)現(xiàn)
(1)HttpClientBuilder 調(diào)用 WebService 接口實(shí)現(xiàn)
1.引入 jar 包
<!-- Jackson Core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.15.0</version> </dependency> <!-- Jackson Databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.0</version> </dependency> <!-- Jackson Dataformat XML --> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.15.0</version> </dependency>
2.業(yè)務(wù)邏輯
package com.ruoyi.system.service; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import java.io.IOException; import com.fasterxml.jackson.core.JsonProcessingException; import com.ruoyi.system.dto.soap.BaseInfoDto; import com.ruoyi.system.dto.soap.InputDataDto; import com.ruoyi.system.dto.soap.OutputDataDto; import com.ruoyi.system.dto.soap.TransDataDto; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.nio.charset.Charset; import java.util.Objects; @Service public class SoapTestService { private static final Logger log = LoggerFactory.getLogger(SoapTestService.class); public String getFinalValidNo(String wsdlUrl, String soapActon) { String finalValidNo = ""; TransDataDto transDataDto = new TransDataDto(); BaseInfoDto baseInfoDto = new BaseInfoDto(); baseInfoDto.setPrjId("1"); baseInfoDto.setUserId("1"); InputDataDto inputDataDto = new InputDataDto(); inputDataDto.setHandCode("1"); inputDataDto.setWriteType("1"); transDataDto.setBaseInfo(baseInfoDto); transDataDto.setInputData(inputDataDto); String soapParam = ""; try { soapParam = transDataDtoToXmlStr(transDataDto); String soapResultStr = doSoapCall(wsdlUrl, soapParam, soapActon); TransDataDto transDataResponse = xmlToTransDataDto(soapResultStr); if (Objects.nonNull(transDataResponse)) { OutputDataDto outputDataDto = transDataDto.getOutputData(); if (!"0".equals(outputDataDto.getResultCode())) { log.error("獲取權(quán)限編號失敗,詳細(xì)信息:{}", outputDataDto.getResultMsg()); throw new RuntimeException(outputDataDto.getResultMsg()); } finalValidNo = outputDataDto.getOrgniseNo(); } } catch (JsonProcessingException jp) { log.error("json 轉(zhuǎn) xml 異常,詳細(xì)錯誤信息:{}", jp.getMessage()); throw new RuntimeException(jp.getMessage()); } return finalValidNo; } /** * @param wsdlUrl:wsdl 地址 * @param soapParam:soap 請求參數(shù) * @param SoapAction * @return */ private String doSoapCall(String wsdlUrl, String soapParam, String SoapAction) { // 返回體 String responseStr = ""; // 創(chuàng)建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); HttpPost httpPost = new HttpPost(wsdlUrl); try { httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); httpPost.setHeader("SOAPAction", SoapAction); StringEntity data = new StringEntity(soapParam, Charset.forName("UTF-8")); httpPost.setEntity(data); CloseableHttpResponse response = closeableHttpClient .execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { // 打印響應(yīng)內(nèi)容 responseStr = EntityUtils.toString(httpEntity, "UTF-8"); log.info("調(diào)用 soap 請求返回結(jié)果數(shù)據(jù):{}", responseStr); } } catch (IOException e) { log.error("調(diào)用 soap 請求異常,詳細(xì)錯誤信息:{}", e.getMessage()); } finally { // 釋放資源 if (closeableHttpClient != null) { try { closeableHttpClient.close(); } catch (IOException ioe) { log.error("釋放資源失敗,詳細(xì)信息:{}", ioe.getMessage()); } } } return responseStr; } /** * @param transDataDto * @return * @throws JsonProcessingException * @Des json 轉(zhuǎn) xml 字符串 */ private String transDataDtoToXmlStr(TransDataDto transDataDto) throws JsonProcessingException { // 將 JSON 轉(zhuǎn)換為 XML 字符串 XmlMapper xmlMapper = new XmlMapper(); StringBuilder stringBuilder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); stringBuilder.append(xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(transDataDto)); return stringBuilder.toString(); } /** * @param xmlResponse * @return * @throws JsonProcessingException * @Des xml 轉(zhuǎn) json */ private TransDataDto xmlToTransDataDto(String xmlResponse) throws JsonProcessingException { // 將 XML 字符串轉(zhuǎn)換為 Java 對象 XmlMapper xmlMapper = new XmlMapper(); TransDataDto transDataDto = xmlMapper.readValue(xmlResponse, TransDataDto.class); return transDataDto; } }
(2)apache axis 方式調(diào)用
1.引入依賴
<dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency>
2.業(yè)務(wù)邏輯
package com.ruoyi.system.service; import org.apache.axis.client.Call; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.apache.axis.client.Service; import java.rmi.RemoteException; @Component public class ApacheAxisTestService { private static final Logger log = LoggerFactory.getLogger(ApacheAxisTestService.class); public String sendWebService() { String requestXml = ""; String soapResponseStr = ""; String wsdlUrl = ""; Service service = new Service(); Object[] obj = new Object[1]; obj[0] = requestXml; log.info("調(diào)用soap接口wsdl地址:{},請求參數(shù):{}", wsdlUrl, requestXml); try { Call call = (Call) service.createCall(); call.setTargetEndpointAddress(wsdlUrl); call.setTimeout(Integer.valueOf(30000)); call.setOperation("doService"); soapResponseStr = (String) call.invoke(obj); } catch (RemoteException r) { log.error("調(diào)用 soap 接口失敗,詳細(xì)錯誤信息:{}", r.getMessage()); throw new RuntimeException(r.getMessage()); } return soapResponseStr; } }
注意?。。。。。?!如果現(xiàn)在開發(fā)WebService,用的大多是axis2或者CXF。
有時候三方給的接口例子中會用到標(biāo)題上面的類,這個在axis2中是不存在,這兩個類屬于axis1中的?。?!
總結(jié)
到此這篇關(guān)于Java(Springboot)項(xiàng)目調(diào)用第三方WebService接口實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Springboot調(diào)用第三方WebService接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring boot webService使用方法解析
- SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類型)
- SpringBoot整合WebService的實(shí)現(xiàn)示例
- SpringBoot創(chuàng)建WebService方法詳解
- SpringBoot調(diào)用第三方WebService接口的兩種方法
- SpringBoot調(diào)用對方webService接口的幾種方法示例
- SpringBoot整合WebService的實(shí)戰(zhàn)案例
- springboot整合webservice使用簡單案例總結(jié)
相關(guān)文章
SpringBoot?對接飛書多維表格事件回調(diào)監(jiān)聽流程分析
本文介紹了如何通過飛書事件訂閱機(jī)制和SpringBoot項(xiàng)目集成,對多維表數(shù)據(jù)的記錄變更進(jìn)行對接的詳細(xì)流程,包括如何創(chuàng)建應(yīng)用、配置參數(shù)、編寫訂閱代碼、訂閱文檔事件以及在SpringBoot工程中集成的步驟,感興趣的朋友跟隨小編一起看看吧2024-12-12java Comparator.comparing排序使用示例
本文主要介紹了java Comparator.comparing排序使用示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10Java對象轉(zhuǎn)JSON時動態(tài)的增刪改查屬性詳解
這篇文章主要介紹了Java對象轉(zhuǎn)JSON時如何動態(tài)的增刪改查屬性的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11SpringBoot集成Beetl后統(tǒng)一處理頁面異常的方法
這篇文章主要介紹了SpringBoot集成Beetl后統(tǒng)一處理頁面異常的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Mybatis-Plus的條件構(gòu)造器QueryWrapper & UpdateWrapper示例詳解
Mybatis-Plus的條件構(gòu)造器QueryWrapper和UpdateWrapper為開發(fā)者提供了強(qiáng)大、靈活的條件構(gòu)建工具,能夠大大簡化數(shù)據(jù)庫操作的代碼,通過本文的介紹,讀者可以更加深入地理解這兩個條件構(gòu)造器的使用方法,并在實(shí)際項(xiàng)目中靈活應(yīng)用,感興趣的朋友跟隨小編一起看看吧2024-01-01mybatis-plus3.0.1枚舉返回為null解決辦法
這篇文章主要介紹了mybatis-plus3.0.1枚舉返回為null解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12