REST Webservices

What is REST?

  • REST means Representational State Transfer  and that  are built around Resources(Java POJO)
  • REST services are Stateless and uses as a Uniform Interface
  • Resources are manipulated through Representations
  • Responses are Representations and they carry or point to current state of application. They may contain links to transition to a new state and application state is not kept on the server.
  • REST Services Maps your CRUD operations to HTTP verbs.These operations are standard via HTTP which can be cached, bookmarked, saved via standard mechanisms
    Action Method Purpose
    Retrive GET Read
    Create POST Create or update if ID is not known
    Update PUT Update or Create if ID is known
    Delete DELETE Remove

REST Implementation frameworks

  1. Apache CXF
  2. Jersey
  3. JBoss RESTEasy
  4. Apache Wink
  5. SpringWeb
  6. Axis etc

JAX-RS Annotations:

In JAX-RS, a Resource is a POJO and matching URI can be expressed with @Path annotation

JAX-RS @Path annotation:

  • @Path on a java  class defines the base relative path for all resources supplied by that class.
  • @Path on a method is relative to any @Path on the class.
  • In the absence of @Path on the class or method, the resource is defined to reside at the root of the service.
  • In the following example, StudentResource defines below URL path
    •  localhost:8080/{application-context-name}/{base-resource}/student-service/get-student
    • localhost:8080/{application-context-name}/{base-resource}/student-service/add-student
  • Base resource is the Jersey Servlet defined in web.xml file
    • <servlet>
  • import;
    public class StudentResource { 
     public String getStudent() {
     return "REST tutorial”;
     public Student addStudent(){

    JAX-RS HTTP Method Annotations:

The below HTTP method annotations are applied to java method to bind to an HTTP method.Only one HTTP annotation may be applied to a single Java method.Multiple Java methods may be given the same HTTP method annotation, assuming they are bound to different paths

  • @GET    –  For get requests
  • @POST  – for POST requests to add new record
  • @PUT  – For update request
  •  @DELETE – For deleting request
  •  @HEAD

JAX-RS Parameter Annotations:

    • @QueryParam – maps to a query string parameter.
    • @FormParam – Extracts from a request representation of MIME media type “application/x-www-form-urlencoded” and conforms to the encoding specified by HTML forms
    • @PathParam – maps to a path segment.
    • @DefaultValue – supplies a default parameter value.The below example gives HOw to use @PathParam and @QueryParam and @DefaultValue annotations

public String getStudent(
 @PathParam("studentId") int studentId,
 @DefaultValue("5") int numResults)
 // Your logic goes here
 public Response editStudent(@FormParam("studentId") Long id, 
 @FormParam("name") String name) {
 // @FormParam example

JAX-RS Other annotations:

  • @Produces  : Specify the MIME media types of representations a resource(applied on class or method level) can produce and send back to the client.
  • @Consumes:  MIME media types of representations a resource can consume that were sent by the client. It can be applied at Class or Method level and one method can consume more than one media type
  • @MatrixParam:Extracts from URL path segments
  • @HeaderParam : to get Request header values
  • @CookieParam : to get values from cookies


Hope this is enough of theory. You can access other sample tutorial here


2 thoughts on “REST Webservices”

Leave a Reply

Your email address will not be published. Required fields are marked *