Skip to main content

9B1.WebService Wsdl

trydoforOriginalPracticeManualAbout 1 min

9B1.WebService Wsdl

Many large systems that provide WebService, such as fedex. In this case, using springboot as a soap client is sufficient.

9B1.1.Wsdl to 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 and Package

  • dep on spring-boot-starter-web-services
  • use maven-jaxb2-plugin
  • Configure xjb for wsdl, specifying the packages separately
<!-- 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>

Then execute mvn compile to automatically generate the code in target/generated-sources. In the operation tag of wsdl, you can find the methods and their requests and responses, and generate the Client as needed.

public class FedexPickupClient extends WebServiceGatewaySupport {

    public CreatePickupReply createPickup(CreatePickupRequest request) {
        return (CreatePickupReply) getWebServiceTemplate().marshalSendAndReceive(request);
    }
}

9B1.3.@XmlRootElement

Method ①, xjc generation, global simple, save time.

<jxb:bindings schemaLocation="path/to/myschema.xsd" node="/xs:schema">
    <jxb:globalBindings>
        <xjc:simple/>
    </jxb:globalBindings>
</jxb:bindings>

Method ②, use ObjectFactory and JAXBElement to wrap the request.

final JAXBElement<PickupAvailabilityRequest> query = objectFactory.createPickupAvailabilityRequest(req);
final JAXBElement<PickupAvailabilityReply> reply = (JAXBElement<PickupAvailabilityReply>) 
getWebServiceTemplate().marshalSendAndReceive(query);
return reply.getValue();

Method ③, configure anno in bindings for each wsdl, the configuration is verbose and not recommended.

9B1.4.Reference