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>
       <servlet-name>jersey-rest-serlvet</servlet-name>
       <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
       <init-param>
       <param-name>com.sun.jersey.config.property.packages</param-name>
       <param-value>org.javasavvy.rest.controller</param-value>
       </init-param>
       <init-param>
       <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
       <param-value>true</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
      </servlet>
      
      <servlet-mapping>
       <servlet-name>jersey-rest-serlvet</servlet-name>
       <url-pattern>/rest/*</url-pattern>
      </servlet-mapping>
  • import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    
    @Path("/student-service")
    public class StudentResource { 
     @GET
     @Produces("text/html") 
     @Path("/get-student")
     public String getStudent() {
     return "REST tutorial”;
     }
     @POST
     @Path("/add-student")
     @Produces("application/json")
     @Consumes("application/json")
     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
  • @OPTIONS
  •  @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
/student-service//get-students/509?results?10

@GET
@Path("/get-students/{studentId}")
@Produces("text/xml")
public String getStudent(
 @PathParam("studentId") int studentId,
 @QueryParam("results")
 @DefaultValue("5") int numResults)
 // Your logic goes here
}
 @POST
 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

 

Comments are closed.