Home » RESTful Webservices » REST Web Service Tutorial-JAX-RS

REST Web Service Tutorial-JAX-RS

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.


Whats Next

In next chapter we will create sample application using JAX-RS & Jersey

Previous Next Article

comments powered by Disqus