Sunday, December 25, 2011

Restful Web Services using JBoss RestEasy

Restful web service is built on top of HTTP protocol. We can say Restful web service is a type of Servlet. I am saying Restful web service is built on top of HTTP protocol because it support all HTTP operations. Initially this was porposed by Roy Fielding who was the main driver of HTTP.

The supported operations of a Restful web service are descrive below:
  • GET
  • POST
  • DELETE
  • PUT
Like JAX-WS, SUN Microsystem introduced JAX-RS for Restful web service. One more important feature of this is totally based on Java Annotation. You can make/published any POGO as a Restful web service, just need to annotate the

You can find number of implementations of JAX-RS. One most popular implementation is RestEasy.This is developed by JBoss community. The RestEasy is comes in two flaver, If you are using JBoss AS 7 then there is no need to add any distribution file for RestEasy. If you are using older version of JBoss, then you have to confugure the RestEasy.

For older version of JBoss, developer have to download a jar file named "resteasy-jaxrs-2.3.0.GA". You can foud this in <>.

Configuration:
  1. After download extract the file and copy all jar files from lib directory to your application.
  2. Modify our Web configuration file(web.xml) file. Add the following lines:
  3. Annotate your POGO for Restful web service.
  4. Deploy the application in JBoss AS.
Resteasy Web Service Annotations: 

@Path - Identifies the URI path that a resource class or class method will serve requests for. Paths are relative. For an annotated class the base URI is the application path, see ApplicationPath. For an annotated method the base URI is the effective URI of the containing class. For the purposes of absolutizing a path against the base URI , a leading '/' in a path is ignored and base URIs are treated as if they ended in '/'

@GET - Indicates that the annotated method responds to HTTP GET requests.

@POST - Indicates that the annotated method responds to HTTP POST requests.

@DELETE - Indicates that the annotated method responds to HTTP DELETE requests

@PUT - Indicates that the annotated method responds to HTTP PUT requests

@Form - This can be used as a value object for incoming/outgoing request/responses. You can re-use @*Param annotations on fields/methods of the parameter to unmarshall from the request or marshall to the response depending if you're using server-side JAX-RS or the Resteasy client framework
When using this on the server side, you must put your @*Param annotations on either fields or setter methods.
When using this with the Resteasy client framework, you must put your @*Param annotations on either fields or getter methods.


@FormParam - Binds the value(s) of a form parameter contained within a request entity body to a resource method parameter. Values are URL decoded unless this is disabled using the Encoded annotation. A default value can be specified using the DefaultValue annotation. If the request entity body is absent or is an unsupported media type, the default value is used.
The type T of the annotated parameter must either:
  1. Be a primitive type
  2. Have a constructor that accepts a single String argument
  3. Have a static method named valueOf or fromString that accepts a single String argument (see, for example, Integer.valueOf(String))
  4. Be List, Set or SortedSet, where T satisfies 2 or 3 above. The resulting collection is read-only.

If the type is not one of those listed in 4 above then the first value (lexically) of the parameter is used.

Note that, whilst the annotation target permits use on fields and methods, this annotation is only required to be supported on resource method parameters.