SpringBoot调用WebService接口的实现示例
作者:m0_74823827
本文主要介绍了SpringBoot调用WebService接口的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
前言
调用WebService接口的方式有很多,今天记录一下,使用 Spring Web Services 调用 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>
二.创建请求类和响应类
根据SOAP的示例,创建请求类和响应类
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>
根据我的这个示例,我创建的请求类和响应类,是这样的
请求类
@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; }
三.创建ObjectFactory类
@XmlRegistry public class ObjectFactory { // 创建 DownloadFileByMaterialCodeRequest 的实例 public DownloadFileByMaterialCodeRequest createDownloadFileByMaterialCodeRequest() { return new DownloadFileByMaterialCodeRequest(); } // 创建 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; } }
五.调用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 // 创建请求对象并设置参数 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); // 发送 SOAP 请求并获取响应 DownloadFileByMaterialCodeResponse response = (DownloadFileByMaterialCodeResponse) webServiceTemplate.marshalSendAndReceive(uri, request, soapActionCallback); // 获取并返回 DownloadFileByMaterialCodeResult String downloadFileByMaterialCodeResult = response.getDownloadFileByMaterialCodeResult(); System.out.println(downloadFileByMaterialCodeResult); //字符串转换为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()); } }
测试效果
这里在附上所有文件的路劲图,可以参考一下
总结
根据接口给出的SAOP的示例,封装好对应的实体类,因为我这里的类型都是String,大家也可以根据实际情况,封装好对应的类
注意注解的参数,namespace = “http://*******/” 给接口提供的域名地址
到此这篇关于SpringBoot调用WebService接口的实现示例的文章就介绍到这了,更多相关SpringBoot调用WebService接口内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!