About Me

My photo
Ernakulam, Kerala, India
I am Sajadh, author of this blog. I run this with loads of passion.
If you are into java, you may find lot of interesting things around ...
Advance thanks for your queries to sajadhaja90@gmail.com.

Wednesday, 2 October 2013

REST with Java (JAX-RS) using Jersey

Refer From :- http://www.vogella.com/articles/REST/article.html

REST is an architectural style which is based on web-standards and the HTTP protocol.
In a REST based architecture everything is a resource. A resource is accessed via a common interface based on the HTTP standard methods.
In a REST based architecture you typically have a REST server which provides access to the resources and a REST client which accesses and modify the REST resources.
Every resource should support the HTTP common operations. Resources are identified by global IDs

HTTP methods
The PUT, GET, POST and DELETE methods are typical used in REST based architectures.

JAX-RS with Jersey

JAX-RS:- Java defines REST support via the Java Specification Request 311

Jersey :- contains basically a REST server and a REST client.

On the server side Jersey uses a servlet which scans predefined classes to identify RESTful resources. Via the web.xml configuration file for your web application, registers this servlet which is provided by the Jersey distribution 

The base URL of this servlet is:
http://your_domain:port/display-name/url-pattern/path_from_rest_class 

This servlet analyzes the incoming HTTP request and selects the correct class and method to respond to this request. This selection is based on annotations in the class and methods.

Table 1. JAX-RS annotations
Annotation
Description
@PATH(your_path)
Sets the path to base URL + /your_path. The base URL is based on your application name, the servlet and the URL pattern from the web.xml" configuration file.
@POST
Indicates that the following method will answer to a HTTP POST request
@GET
Indicates that the following method will answer to a HTTP GET request
@PUT
Indicates that the following method will answer to a HTTP PUT request
@DELETE
Indicates that the following method will answer to a HTTP DELETE request
@Produces(MediaType.TEXT_PLAIN [, more-types ])
@Produces defines which MIME type is delivered by a method annotated with @GET. In the example text ("text/plain") is produced. Other examples would be "application/xml" or "application/json".
@Consumes(type [, more-types ])
@Consumes defines which MIME type is consumed by this method.
@PathParam
Used to inject values from the URL into a method parameter. This way you inject for example the ID of a resource into the method to get the correct object.
Please Refer Site : - http://www.vogella.com/articles/REST/article.html