REST Web Service Tutorial-JAX-RS

What is JAX-RS ?

JAX-RS stands for Java API for RESTful Web Services (JAX-RS), is a set of APIs to develop REST services. JAX-RS is part of the Java EE6, and make developers to develop REST web application easily.JAX-RS makes heavy use of annotations available from Java SE 5 to simplify development of JAVA based web services.

There are two main implementation of JAX-RS API.

  1. Jersey
  2. RESTEasy

What is REST ?

REST stands for Representational State Transfer.REST uses HTTP Protocol for data communication. REST relies on stateless, client server and cachebale communication protocol. REST is web standards based architecture for designing networked applications.

REST was introduced and defined by Roy Fielding in 2000. The idea is rather than using complex mechanism such as CORBA,RPC or SOAP to connect applications, simple HTTP protocol is used.

RESTful application use HTTP requests to send data(Create/Update),read data and delete data. Thus REST uses HTTP for all four CRUD operations.



Following are the commonly used annotations in JAX-RS to map a resource as a web service resource.

AnnotationDescription
@Path The @Path annotation’s value is a relative URI path indicating where the Java class will be hosted: for example, /helloworld. You can also embed variables in the URIs to make a URI path template. For example, you could ask for the name of a user and pass it to the application as a variable in the URI: /helloworld/{username}.
@GET This annotation indicates that the following method should respond to the HTTP GET request only.HTTP Get request, used to fetch resource.
@POST This annotation indicates that the following method should respond to the HTTP POST request only.HTTP POST request is used to create/update resource.
@PUT This annotation indicates that the following method should respond to the HTTP PUT request only.HTTP PUT request is used to create resource.
@DELETE This annotation indicates that the following method should respond to the HTTP DELETE request only.HTTP DELETE request is used to delete resource.
@HEAD This annotation indicates that the following method should respond to the HTTP HEAD request only.HTTP HEAD request is used to get status of method availability.
@Produces(MediaTypes.TEXT_PLAIN[,more –types]) It 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]) It defines MIME type which is consumed by this method.
@PathParam Used to inject values from the URL into a method parameter. For example you can inject id of the resource into the method to get the correct object.


JAX-RS and Jersey

Jersey is the open source reference implementation of Java JAX-RS specification. It provides a Java library using which we can easily create RESTful web services in Java platform. JAX-RS / Jersey supports JAXB based XML bindings.




Related Articles

  1. Maven Overview



comments powered by Disqus