Saturday, April 12, 2008

Developing JAX-WS based Web Services using Java WSDP

After developing web services using JAX-RPC, I shifted my attention to another web service standard for Java - JAX-WS (Java API for XML based web services). It is a successor to JAX-RPC 1.1, and obviously, it enjoys some advantages over its predecessor.

The major differences between the two standards are:
  1. JAX-WS supports SOAP 1.2, and XML over HTTP.
  2. JAX-RPC supports WS-I's Basic Profile (BP) version 1.0. JAX-WS supports BP 1.1. (WS-I is the Web services interoperability organization.)
  3. JAX-RPC maps to Java 1.4. JAX-WS maps to Java 5.0. JAX-WS relies on many of the features new in Java 5.0.
  4. JAX-WS's data mapping model is JAXB, unlike JAX-RPC. which create LiteralSerializer classes for all the data elements.
  5. JAX-RPC handlers rely on SAAJ 1.2. JAX-WS handlers rely on the new SAAJ 1.3 specification.
  6. JAX-WS heavily relies on annotations.
Now, for the implementation. I created a sample web service application based on JAX-WS (the pack comes with the Java WSDP). To develop a web service, two tasks are provided: wsimport and wsgen.

wsimport creates service artifacts and client stubs by looking into the WSDL. wsgen reads a web service endpoint class and generates all the required artifacts for web service deployment, and invocation. Both need to be used together to create a web service.

The steps that need to be followed for creating a web service and deploying it can be listed down as follows (refer to the build.xml in the uploaded zip file):
  1. Create the WSDL (assuming top-down approach) .
  2. Generate the service interfaces using wsimport. Additionally, server binding configuration xml can be used to specify the namespace mapping with Java packages, and specifying the server side handlers. Refer to the ant task - create-server-wsdl.
  3. Implement service endpoint using the interface.
  4. Generate the service artifacts from the implemented class using wsgen. This generally comprises of JAXB objects.
  5. Create a war file, which has the following structure:
    • WEB-INF/classes
    • WEB-INF/lib
    • WEB-INF/web.xml
    • WEB-INF/sun-jaxws.xml
    • WEB-INF/wsdl/wsdl-file.wsdl
    (For steps 4 and 5, refer to the ant task - build-war)
The sun-jaxws.xml is an additional file which needs to be created. It specifies the end point interfaces, the implementation class, the port, and the url pattern for the service, which is used by the container in which the service is deployed.

Once the war is created, it can be deployed on any web server/ application server. I tested it on both Tomcat web server 5 and Sun Application Server 9.

The client stubs can be generated using the wsimport and specifying the WSDL file. (ant task - create-client-wsdl). Again, the binding configuration file for the client can be used to provide namespace mapping and client side handlers.

As with JAX-RPC implementation, the wsimport and wsgen tasks are dependent on a number of configuration xml files. But unlike the wsdeploy task in JAX-RPC, where a valid web.xml deployment descriptor is created with a listener class and a JAX-RPC servlet automatically, here, in JAX-WS, these entries have to be manually entered. Also, there is a heavy use of annotations, and JAXB objects.

The sample application, together with the build script is uploaded as

No comments:

Post a Comment