前言
调用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 {@Beanpublic WebServiceTemplate webServiceTemplate() {Jaxb2Marshaller marshaller = new Jaxb2Marshaller();marshaller.setContextPath("org.jeecg.modules.webservice"); // 包路径,包含请求和响应对象类WebServiceTemplate template = new WebServiceTemplate(marshaller);return template;}
}
五.调用WebService接口
@Service
public class DownloadFileService {@Autowiredprivate 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);// 设置 SOAPActionString soapAction = "http://********/DownloadFileByMaterialCode"; // Web 服务指定的 SOAPAction// 使用 SoapActionCallback 来设置 SOAPAction 头SoapActionCallback soapActionCallback = new SoapActionCallback(soapAction);// 发送 SOAP 请求并获取响应DownloadFileByMaterialCodeResponse response = (DownloadFileByMaterialCodeResponse)webServiceTemplate.marshalSendAndReceive(uri, request, soapActionCallback);// 获取并返回 DownloadFileByMaterialCodeResultString 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;}
}
六.测试代码
@Testpublic 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://*******/” 给接口提供的域名地址