Sunday, January 20, 2013

Java Web Services – bottom-top-down approach

There are two approaches to developing web services: bottom-up and top-down. In the bottom-up approach, annotated Java classes are created first, and the WSDL is generated from these classes using wsgen tool.
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.
However, top-down approach is the recommended approach as it provides better control over design, and consequently greater degree of interoperability. Therefore, to develop web services quickly without compromising interoperability, I use what I call the bottom-top-down approach.

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:
@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.");
}
}
Then, we generate the WSDL using jaxws-maven-plugin. Build section of the pom is shown below:
<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>
view raw pom.xml hosted with ❤ by GitHub

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.

<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>
view raw pom.xml hosted with ❤ by GitHub
Generated Java classes use XMLGregorianCalendar instead of java.util.Calendar for XML date and time types. This can be customized using a binding file. In addition, we can make our generated classes implement the Serializable interface. An example binding file is illustrated below:
<?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>
view raw binding.xml hosted with ❤ by GitHub

Source code for these projects can be found in the quickstarts repository at GitHub.

2 comments:

  1. Hi Nazar. Good post. :)
    Have you tried making the schemas using xmlspy? If not, you should, as it might change your view on the top-down-approach.

    ReplyDelete
  2. I haven't tried XMLSpy, I will check out the demo, thanks for the tip :)

    ReplyDelete