Saturday, 3 August 2013

REST - Representational State Transfer


REST-Introduction

REST is short form of Representational State Transfer
Rest is architectural style web service which is based on web standards and http protocols(get,post,put,delete).REST was invented by Roy Fielding in 2000.In REST based architecture everything consider as resource, these resources   access by common interface based on the HTTP standard methods(get,post,put,delete)
In REST architecture you have server which provide and controls the access to the resources and REST client can access and modify the resources.REST support all the common http operations and resources are accessed by URI
REST allow that resources have different representational styles such as TEXT,HTML,XML,JSON,etc.,Client access the   these representation  by using URI.
We can create REST ful web service using following methods
  1. Jersey
  2. REST Easy
We will see these in upcoming chapters.

HTTPS Methods 

  1. GET
  2. PUT
  3. POST
  4. DELETE
  • GET -Its defines the reading access of the resource.The resource never get modified by GET request it can only used to access the resources
  • PUT- It is used to create new resource.
  • POST- Its is used to update existing resource or create new resource
  • DELETE – It is used to remove the resources

RESTful webservice

RESTful webservice are based on REST Architecture and HTTP methods.It is URI bassed service.It support MIME Types(TEXT,XML,HTML,JSON and user defined Objects )

JAX-RS Using Jersey

Java support REST via JSR 311(Java Specification Request).This is called JAX-RX.JAX-RX uses annotation to define REST resources.Jersey contains server and client.Client used to communicate with server
In server side jersey contains servlet which scans the predefined class to identify the RESTful resouces vai web.xml.Jersey servlet configure in web.xml.The base URL of this servlet name
http://domain-name/your project name/url pattern/ path in your class
The REST servlet receive the incoming then select correct class and methods based on  annotations mentioned in the class and produce the result in mentioned type.Rest support Creation of XML and JSON Web service using JAXB(Java Architecture for XML Binding).We will see JAXB in separate chapter

JAX-RX annotations

See below table for few important annotations used in Web Service.JAX-RX contains more annotations
Annotaion
Description
@PATH(path) Set path to your class and methods.This used to call the resource by using base url + path
@POST HTTP Post method.respond with HTTP POST request
@GET HTTP Get method.respond with HTTP GET request
@PUT HTTP Put method.respond with HTTP PUT request
@DELETE HTTP Delete method.respond with HTTP DELETE request
@Produces(any MIME TYPE) @Produces defines which MIME type is returned by a method annotated with @GET.  example "text/plain,"application/xml" or "application/json",etc.
@Consumes(any MIME TYPE) @Consumes media type consumed by method
@PathParam pass the value in url.eg. /pathparam
@QueryParam Append the parameters with url query param
Jersey Jars
Jersey  jars avaiable at
http://jersey.java.net/ 



Download these jars before proceeding with examples.In next chapter we will see simple Hello World RESTful web service using JAX-RS Jersy




0 comments

Post a Comment