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 RESTful web service using following methods
- REST Easy
We will see these in upcoming chapters.
RESTful Web services
RESTful Web services that seamlessly support exposing your data in a variety of representation media types and abstract away the low-level details of the client-server communication is not an easy task without a good toolkit. In order to simplify development of RESTful Web services and their clients in Java, a standard and portable JAX-RS API has been designed.
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 )
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
JAX-RS Using Jersey
Jersey RESTful Web Services framework is open source, production quality, framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.
Jersey framework is more than the JAX-RS Reference Implementation. Jersey provides it’s own API that extend the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development. Jersey also exposes numerous extension SPIs so that developers may extend Jersey to best suit their needs.
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
See below table for few important annotations used in Web Service. JAX-RX contains more annotations
|@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|
Posts You may Like
- How to convert a String to Upper case
- How to add value to all types of Collections
- Java Best Practice Naming Conventions
- Java 7 new features
- Java Best Practice – File Organization
- How to read all the files from folder using File.list() in Java
- How to Read File using BufferedReader
- How to convert Object to JSON using Jackson 1.x
- Email Receiver using Java Mail API
- Java Best Practice Comments
- Java String Compare
- How to search a word inside String
- Java 8 – filter the null values from Stream Example
- How to Read JSON from File
- Create Pretty Print JSON using GSON