SpringBoot 怎样调用 WebService 接口

打印 上一主题 下一主题

主题 1839|帖子 1839|积分 5527

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x


媒介

调用WebService接口的方式有很多,今天记载一下,利用 Spring Web Services 调用 SOAP WebService接口
一.导入依靠

  1.         <!-- Spring Boot Web依赖 -->
  2.         <dependency>
  3.             <groupId>org.springframework.boot</groupId>
  4.             <artifactId>spring-boot-starter-web</artifactId>
  5.         </dependency>
  6.         <!-- Spring Web Services -->
  7.         <dependency>
  8.             <groupId>org.springframework.ws</groupId>
  9.             <artifactId>spring-ws-core</artifactId>
  10.         </dependency>
  11.         <!-- Apache HttpClient 作为 WebService 客户端 -->
  12.         <dependency>
  13.             <groupId>org.apache.httpcomponents</groupId>
  14.             <artifactId>httpclient</artifactId>
  15.         </dependency>
  16.         <!-- JAXB API -->
  17.         <dependency>
  18.             <groupId>javax.xml.bind</groupId>
  19.             <artifactId>jaxb-api</artifactId>
  20.             <version>2.3.1</version>
  21.         </dependency>
  22.         <!-- JAXB 运行时 -->
  23.         <dependency>
  24.             <groupId>org.glassfish.jaxb</groupId>
  25.             <artifactId>jaxb-runtime</artifactId>
  26.             <version>2.3.5</version>
  27.         </dependency>
复制代码
二.创建请求类和相应类

根据SOAP的示例,创建请求类和相应类
SOAP示例
  1. 请求
  2. POST *****************
  3. Host: **************
  4. Content-Type: text/xml; charset=utf-8
  5. Content-Length: length
  6. SOAPAction: "http://*******/DownloadFileByMaterialCode"
  7. <?xml version="1.0" encoding="utf-8"?>
  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/">
  9.   <soap:Body>
  10.     <DownloadFileByMaterialCode xmlns="http://*******/">
  11.       <MaterialCode>string</MaterialCode>
  12.       <FileType>string</FileType>
  13.       <Category>string</Category>
  14.     </DownloadFileByMaterialCode>
  15.   </soap:Body>
  16. </soap:Envelope>
  17. 响应
  18. HTTP/1.1 200 OK
  19. Content-Type: text/xml; charset=utf-8
  20. Content-Length: length
  21. <?xml version="1.0" encoding="utf-8"?>
  22. <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/">
  23.   <soap:Body>
  24.     <DownloadFileByMaterialCodeResponse xmlns="http://********/">
  25.       <DownloadFileByMaterialCodeResult>string</DownloadFileByMaterialCodeResult>
  26.     </DownloadFileByMaterialCodeResponse>
  27.   </soap:Body>
  28. </soap:Envelope>
复制代码
根据我的这个示例,我创建的请求类和相应类,是如许的
请求类
  1. @Data
  2. @XmlRootElement(name = "DownloadFileByMaterialCode", namespace = "http://*******/")
  3. @XmlAccessorType(XmlAccessType.FIELD)
  4. public class DownloadFileByMaterialCodeRequest {
  5.     @XmlElement(name = "MaterialCode", namespace = "http://*******/")
  6.     private String MaterialCode;
  7.     @XmlElement(name = "FileType", namespace = "http://*******/")
  8.     private String FileType;
  9.     @XmlElement(name = "Category", namespace = "http://*******/")
  10.     private String Category;
  11. }
复制代码
相应类
  1. @Data
  2. @XmlRootElement(name = "DownloadFileByMaterialCodeResponse", namespace = "http://********/")
  3. @XmlAccessorType(XmlAccessType.FIELD)
  4. public class DownloadFileByMaterialCodeResponse {
  5.     @XmlElement(name = "DownloadFileByMaterialCodeResult", namespace = "http://********/")
  6.     private String DownloadFileByMaterialCodeResult;
  7. }
复制代码
三.创建ObjectFactory类

  1. @XmlRegistry
  2. public class ObjectFactory {
  3.     // 创建 DownloadFileByMaterialCodeRequest 的实例
  4.     public DownloadFileByMaterialCodeRequest createDownloadFileByMaterialCodeRequest() {
  5.         return new DownloadFileByMaterialCodeRequest();
  6.     }
  7.     // 创建 DownloadFileByMaterialCodeResponse 的实例
  8.     public DownloadFileByMaterialCodeResponse createDownloadFileByMaterialCodeResponse() {
  9.         return new DownloadFileByMaterialCodeResponse();
  10.     }
  11. }
复制代码
四.设置WebServiceTemplate

  1. @Configuration
  2. public class WebServiceConfig {
  3.     @Bean
  4.     public WebServiceTemplate webServiceTemplate() {
  5.         Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
  6.         marshaller.setContextPath("org.jeecg.modules.webservice");  // 包路径,包含请求和响应对象类
  7.         WebServiceTemplate template = new WebServiceTemplate(marshaller);
  8.         return template;
  9.     }
  10. }
复制代码
五.调用WebService接口


  1. @Service
  2. public class DownloadFileService {
  3.     @Autowired
  4.     private WebServiceTemplate webServiceTemplate;
  5.     public List<ResponseJsonObject> downloadFile(String materialCode, String fileType, String category) throws JsonProcessingException {
  6.         String uri = "http://192.168.***.***/DYDServiceTest/PlmService.asmx";  // WebService 的 URL
  7.         // 创建请求对象并设置参数
  8.         DownloadFileByMaterialCodeRequest request = new DownloadFileByMaterialCodeRequest();
  9.         request.setMaterialCode(materialCode);
  10.         request.setFileType(fileType);
  11.         request.setCategory(category);
  12.         // 设置 SOAPAction
  13.         String soapAction = "http://********/DownloadFileByMaterialCode";  // Web 服务指定的 SOAPAction
  14.         // 使用 SoapActionCallback 来设置 SOAPAction 头
  15.         SoapActionCallback soapActionCallback = new SoapActionCallback(soapAction);
  16.         // 发送 SOAP 请求并获取响应
  17.         DownloadFileByMaterialCodeResponse response = (DownloadFileByMaterialCodeResponse)
  18.                 webServiceTemplate.marshalSendAndReceive(uri, request, soapActionCallback);
  19.         // 获取并返回 DownloadFileByMaterialCodeResult
  20.         String downloadFileByMaterialCodeResult = response.getDownloadFileByMaterialCodeResult();
  21.         System.out.println(downloadFileByMaterialCodeResult);
  22.         //字符串转换为ResponseJsonObject对象
  23.         ObjectMapper objectMapper = new ObjectMapper();
  24.         // 忽略未知字段
  25.         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  26.         List<ResponseJsonObject> ResponseJsonObjects = objectMapper.readValue(downloadFileByMaterialCodeResult, objectMapper.getTypeFactory().constructCollectionType(List.class, ResponseJsonObject.class));
  27.         return ResponseJsonObjects;
  28.     }
  29. }
复制代码
 六.测试代码

  1.     @Test
  2.     public void test1() throws JsonProcessingException {
  3.         List<ResponseJsonObject> responseJsonObjects = downloadFileService.downloadFile("CCPT0016-QBY-7", "", "");
  4.         for (ResponseJsonObject responseJsonObject : responseJsonObjects) {
  5.             System.out.println(responseJsonObject.getDocName());
  6.         }
  7.     }
复制代码
测试效果


这里在附上全部文件的路劲图,可以参考一下

总结

根据接口给出的SAOP的示例,封装好对应的实体类,因为我这里的类型都是String,大家也可以根据实际情况,封装好对应的类
留意注解的参数,namespace = "http://*******/"  给接口提供的域名地址

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

泉缘泉

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表