SOAP接口对接
SOAP协议
SOAP (简单对象访问协议)是基于xml格式简易协议,支持绑定http、tcp、udp协议进行网络信息交换。常见的是SOAP结合http构建rpc风格的传输,大致过程是SOAP消息基于xml进行编码,然后映射到http协议语义上,基于http协议进行消息的网络传输。(也可以将SOAP消息映射到tcp\udp上)
webservice
webservice由SOAP、WSDL、UDDI 组成,SOAP用来描述传递信息的格式、WSDL用来描述如何访问具体的接口,UDDI用来管理查询webservice。
spring webservice 实现
发布webservice
https://spring.io/guides/gs/producing-web-service/#initial
访问webservice
- 生成相应的服务依赖对象,通过服务发放地址(通常拼接上?wsdl)可以获取相应的wsdl文件,利用maven的 maven-jaxb2-plugin插件生成对象
参考
<build> <plugins> <!-- tag::wsdl[] --> <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.14.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <schemaLanguage>WSDL</schemaLanguage> <generateDirectory>${project.basedir}/src/main/java</generateDirectory> <generatePackage>com.xxx.xxxxx</generatePackage> <schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory> <schemaIncludes> <include>xx.wsdl</include> </schemaIncludes> <!-- <schemas>--> <!-- <schema>--> <!-- <url>http:xxxxxxxx?wsdl</url>--> <!-- </schema>--> <!-- </schemas>--> </configuration> </plugin> <!-- end::wsdl[] --> </plugins> </build>
- 基于spring提供的webserviceTemplate 可以简单便捷的进行接口的请求
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
- webserviceTemplate 使用
WebServiceTemplate webServiceTemplate = new WebServiceTemplate() ; Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath("com.xx.xxx");//marshaller 解析的时候会依赖这个类路径 webServiceTemplate.setMarshaller(marshaller); webServiceTemplate.setUnmarshaller(marshaller); ObjectFactory objectFactory = new ObjectFactory(); return ((JAXBElement<QueryPolicyInfoResponse>)webServiceTemplate.marshalSendAndReceive(url, objectFactory.createXXXX(xxxxx))).getValue();
参考
https://www.w3school.com.cn/soap/soap_intro.asp
https://spring.io/guides/gs/producing-web-service/#initial
https://docs.spring.io/spring-ws/site/reference/html/client.html