9B1.WebService服务
原创实战手册大约 1 分钟
9B1.WebService服务
很多大系统,提供了WebService服务,比如fedex。 此时,使用springboot作为soap client即可。
9B1.1.wsdl生成java
<!-- pom.xml -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaIncludes>
<include>wsdl-client/fedex/advanced/PickupService_v22.wsdl</include>
<include>wsdl-client/fedex/advanced/ShipService_v26.wsdl</include>
<include>wsdl-client/fedex/standard/RateService_v28.wsdl</include>
<include>wsdl-client/fedex/standard/TrackService_v19.wsdl</include>
</schemaIncludes>
<bindingIncludes>
<include>wsdl-client/bind/fedex.xjb</include>
</bindingIncludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
9B1.2.wsdl和package
- 依赖
spring-boot-starter-web-services
- 使用
maven-jaxb2-plugin
- 为wsdl配置xjb,分别指定包
<!-- wsdl-client/bind/fedex.xjb -->
<jxb:bindings version="2.1"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
jxb:extensionBindingPrefixes="xjc">
<jxb:globalBindings>
<xjc:simple/>
<jxb:javaType xmlType="xs:integer" name="java.lang.Integer"/>
<jxb:javaType xmlType="xs:positiveInteger" name="java.lang.Integer"/>
<jxb:javaType xmlType="xs:nonNegativeInteger" name="java.lang.Integer"/>
<xjc:javaType xmlType="xs:date" name="java.time.LocalDate" adapter="pro.fessional.mirana.jaxb.LocalDateXmlAdapter"/>
<xjc:javaType xmlType="xs:time" name="java.time.LocalTime" adapter="pro.fessional.mirana.jaxb.LocalTimeXmlAdapter"/>
<xjc:javaType xmlType="xs:dateTime" name="java.time.LocalDateTime" adapter="pro.fessional.mirana.jaxb.LocalDateTimeXmlAdapter"/>
<xjc:javaType xmlType="xs:duration" name="java.time.Duration" adapter="pro.fessional.mirana.jaxb.DurationXmlAdapter"/>
</jxb:globalBindings>
<jxb:bindings schemaLocation="../fedex/advanced/PickupService_v22.wsdl" node="//xs:schema">
<jxb:schemaBindings>
<jxb:package name="com.moilioncircle.fedex.wsdl.pickup"/>
</jxb:schemaBindings>
</jxb:bindings>
<jxb:bindings schemaLocation="../fedex/advanced/ShipService_v26.wsdl" node="//xs:schema">
<jxb:schemaBindings>
<jxb:package name="com.moilioncircle.fedex.wsdl.ship"/>
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
之后执行mvn compile
可自动在target/generated-sources
生成代码。 在wsdl
的operation
标签中,可以找到方法及其request和response,根据需要生成Client
public class FedexPickupClient extends WebServiceGatewaySupport {
public CreatePickupReply createPickup(CreatePickupRequest request) {
return (CreatePickupReply) getWebServiceTemplate().marshalSendAndReceive(request);
}
}
9B1.3.@XmlRootElement
方法①,xjc生成时,全局simple,省事。
<jxb:bindings schemaLocation="path/to/myschema.xsd" node="/xs:schema">
<jxb:globalBindings>
<xjc:simple/>
</jxb:globalBindings>
</jxb:bindings>
方法②,使用ObjectFactory和JAXBElement包一下请求,需要封装。
final JAXBElement<PickupAvailabilityRequest> query = objectFactory.createPickupAvailabilityRequest(req);
final JAXBElement<PickupAvailabilityReply> reply = (JAXBElement<PickupAvailabilityReply>)
getWebServiceTemplate().marshalSendAndReceive(query);
return reply.getValue();
方法③,在bindings为每个wsdl配置anno,配置麻烦,不推荐。