Wednesday, March 16, 2011

JAX-WS explained in layman terms with an example

    JAX-WS, often touted as the "Holy Grail" of Java Web Services, is the most popular technology available in recent years; however what is disappointing is the lack of any detailed information that tries to clearly explain in layman terms what is JAX-WS? In this article I try to explain to the best what "JAX-WS" is with an example.

JAX-WS -> is a specification (JSR 224) provided by Sun which consists of a set of API used for creating Web Services. We have heard this statement in almost every web site but what this really means?

JAX-WS, like all specifications, is divided into 2 parts:

  1. The First part consists of an API provided by SUN. This API is accompanied by a JSR. The API consists of a set of interfaces, annotations, xml files and abstract classes (sometimes maybe a few base classes) which are to be used when using that feature. One very important thing about this API that should be noted is that the API in itself is not capable of achieving the intended objective of the specification. The JSR lays down a set of rules and regulations governing the API.
  2. The Second part of any specification is the implementation. The implementation follows all the rules laid down by the JSR and uses the API (i.e. provides all implementation for interfaces, and classes that use the annotations to get the desired objective) to provide a base. For example, in EJB 3.0 by annotating a class by @Stateless annotation we can create an EJB. The @Stateless annotation is useless by itself, however when this class is deployed on an EJB 3.0 enabled server (which contains an implementation for EJB 3.0 API) the server makes sense of the annotation and creates the corresponding EJB and gives the desired result.
Similarly in the case of JAX-WS, we have 2 parts: the API which is available from SUN and multiple reference implementations by different vendors like: Sun (JDK 1.6), Apache (CXF, AXIS 2), IBM (Web sphere), ORACLE (Weblogic App Server), Glassfish (Metro).

How to create a Web Services: As discussed in a previous article JAX-WS supports Java First, WSDL First and Meet in the Middle approach. However for the most basic discussion, I shall be using a Java First approach to create a sample web service. A few of the important features that JAX-WS provides are:

  1. JAXB data binding framework which can be switched ON or OFF.
  2. Automatic generation of WSDL when using Java First
  3. Automatic generation of Binding data when using Java First
  4. Automatic generation of skeleton classes when using WSDL First
  5. Automatic generation of Binding when using WSDL First.
  6. Customization for both Java First and WSDL First approaches.
The simplest way to create a web service consists of the following steps:

  1. Create an implementation class
  2. Annotate the class with @WebService annotation
  3. Annotate the methods to be exposed with @WebMethod annotation
  4. Generate WSDL, XSD and Binding classes using wsgen functionality (glassfish)
  5. Deploy the service on server (Glassfish)
  6. Get WSDL
  7. Test service using Eclipse Web Service Explorer
Check out the step by step illustrated JAX-WS example here.

1 comment: