March 05, 2009

Jersey Annotations explained!

Jersey uses Java programming language annotations to simplify the development of RESTful web services. Developers decorate a resource class (a POJO) with HTTP-specific annotations to define resources and the actions that can be performed on those resources. Like I promised in the previous post, here we will be explaining about different annotations a developer needs to know when using Jersey.

Jersey provides just about 19 annotations. Lets get started right away…

@Path – Path annotation value is a relative URI path to the resource. A resource class must have this annotation either at the class level or for the methods defined in the resource class. The annotation also provides the facility for embedding variables and creating URI path templates.

@Consumes – This annotation specifies the media types that the methods of a resource class can accept. It’s an optional one and by default, the container assumes that any media type is acceptable. This annotation can be used to filter the requests sent by the client. On receiving request with wrong media type, sever throws an error to the client.

@Produces – This annotation defines the media types that the methods of a resource class can produce. Like @Consumes annotation, this is also optional and by default the container assumes that any media type can be sent back to the client.

Jersey also provides annotations to specify the request methods and supports 5 methods. They are:

@GET – This is request method designator corresponding to HTTP GET method. Any java method annotated with this will process HTTP GET requests.

@POST – This is request method designator corresponding to HTTP POST method. Any java method annotated with this will process HTTP POST requests.

@PUT – This is request method designator corresponding to HTTP PUT method. Any java method annotated with this will process HTTP PUT requests.

@DELETE – This is request method designator corresponding to HTTP DELETE method. Any java method annotated with this will process HTTP DELETE requests.

@HEAD – This is request method designator corresponding to HTTP HEAD method. Any java method annotated with this will process HTTP HEAD requests.

@HttpMethod – This annotation is another way of specifying HTTP request method. The annotation takes a value which can be any of supported HTTP request method.

Example:

HttpMethod(“GET”)
Public String getUserName() {
return username;
}

@DefaultValue – The DefaultValue annotation is used to define default values of request metadata. The annotation is bound to be used along with PathParam, QueryParam, MatrixParam, CookieParam, FormParam, or HeaderParam annotations. The defaultValue is used if corresponding metadata is not available with the request.

@Context – The Context annotation is used to inject information into a variable or method parameter. Using this annotation, you will be able to inject information like Request, URI information etc.

To extract information from URL,request hearders etc, Jersey provide a set of annotations that make extracting data very simple. They are:

@PathParam – The PathParam annotation is used to extract parameters from the resource URL. URI path parameters are extracted from the request URI, and the parameter names correspond to the URI path template variable names specified in the @Path class-level annotation.

@QueyParam – This annotation is used to extract parameters from URL query parameters. It is mostly used in conjunction with HTTP GET method. When using GET method to send data, the parameters and values are encoded into the URL. QueryParam is used to extract these values and assign them to the appropriate variables.

@FormParam – This annotation is used to extract parameters from a form submission. It is used in conjunction with HTTP POST method.

@CookieParam – Like above mentioned parameter extraction annotations, CookieParam is used to extract values form a cookie.

@HeaderParam – This annotation is used to extract parameters from HTTP header.

@MatrixParam – This annotation is used to extract parameters from matrix URI. You can learn more about matrix URI here.

@Provider – This annotation is used to mark the implementation of an extension interface.

@Encoded – This annotation is used to disable automatic decoding of parameter values. Is this annotation on a method will disable decoding for all parameters.

Well that's all about Jersy annotations. I have some important points for you to remember associated with annotations:

  • Method level annotations override a class level annotation.
  • It is possible to define more that one Media type in @Consumes and @Produces annotations.


I will be back really interesting article related building RESTful service next week.Till them enjoy programming :)

No comments :