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:
Then, we generate the WSDL using jaxws-maven-plugin. Build section of the pom is shown below:
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.

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:

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