![]() | |
Bottom-up approach. |
In the top-down approach, the WSDL is created first, and the Java artifacts are generated using wsimport tool.
![]() | |
Top-down approach. |
As a Java developer, I find the bottom-up approach simpler and quicker because:
- Java classes are faster to create than XML schema
- Creating a skeleton for a service endpoint interface (SEI) is faster than creating a WSDL skeleton.
In this approach, I very quickly create Java classes including the skeleton for the SEI, annotate them with basic JAX-WS annotations and generate a WSDL. Then, I customize the generated WSDL to be WS-I compliant (see also Which style of WSDL should I use?) and regenerate Java classes using wsimport.
An example using Maven
Let's create a simple maven project with several model classes and a SEI implementation class:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@WebService | |
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED) | |
public class MovieSearchServiceImpl implements MovieSearchService { | |
@WebMethod | |
@Override | |
public SearchResult search(Filter filter) { | |
throw new UnsupportedOperationException("Not supported yet."); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.jvnet.jax-ws-commons</groupId> | |
<artifactId>jaxws-maven-plugin</artifactId> | |
<version>2.1</version> | |
<executions> | |
<execution> | |
<goals> | |
<goal>wsgen</goal> | |
</goals> | |
</execution> | |
</executions> | |
<configuration> | |
<genWsdl>true</genWsdl> | |
<verbose>true</verbose> | |
<sei>com.example.MyServiceImpl</sei> | |
</configuration> | |
<dependencies> | |
<dependency> | |
<groupId>com.sun.xml.ws</groupId> | |
<artifactId>jaxws-rt</artifactId> | |
<version>2.1.7</version> | |
</dependency> | |
</dependencies> | |
</plugin> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.0</version> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> |
The generated WSDL file can be found under target/jaxws/wsgen/wsdl.
Then, we customize the namespaces and verify that the WSDL doesn't have Java specific parts. Then, we are ready to re-generate our Java classes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.jvnet.jax-ws-commons</groupId> | |
<artifactId>jaxws-maven-plugin</artifactId> | |
<version>2.1</version> | |
<executions> | |
<execution> | |
<goals> | |
<goal>wsimport</goal> | |
</goals> | |
</execution> | |
</executions> | |
<configuration> | |
<keep>true</keep>> | |
<verbose>true</verbose> | |
<wsdlDirectory>src/main/resources/wsdl</wsdlDirectory> | |
<!-- | |
<bindingDirectory>src/main/resources/binding</bindingDirectory> | |
--> | |
</configuration> | |
<dependencies> | |
<dependency> | |
<groupId>com.sun.xml.ws</groupId> | |
<artifactId>jaxws-rt</artifactId> | |
<version>2.1.7</version> | |
</dependency> | |
</dependencies> | |
</plugin> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.0</version> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" | |
xmlns:xs="http://www.w3.org/2001/XMLSchema" | |
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" | |
version="2.1" jaxb:extensionBindingPrefixes="xjc"> | |
<jaxb:globalBindings> | |
<!-- Use java.util.Calendar instead of javax.xml.datatype.XMLGregorianCalendar for xs:dateTime --> | |
<jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" | |
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" | |
printMethod="javax.xml.bind.DatatypeConverter.printDateTime" /> | |
<!-- Use java.util.Calendar instead of javax.xml.datatype.XMLGregorianCalendar for xs:date --> | |
<jaxb:javaType name="java.util.Calendar" xmlType="xs:date" | |
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" | |
printMethod="javax.xml.bind.DatatypeConverter.printDateTime" /> | |
<!-- Generated classes should implement Serializable interface --> | |
<xjc:serializable uid="1" /> | |
<xjc:simple /> | |
</jaxb:globalBindings> | |
</jaxb:bindings> |
Source code for these projects can be found in the quickstarts repository at GitHub.
Hi Nazar. Good post. :)
ReplyDeleteHave you tried making the schemas using xmlspy? If not, you should, as it might change your view on the top-down-approach.
I haven't tried XMLSpy, I will check out the demo, thanks for the tip :)
ReplyDelete