About Me
Facebook
Facebook
Linked In
Linked In
Twitter
Twitter
YouTube
YouTube
Google +
Google +

September 23, 2015

Introduction to RESTful Web Services



Introduction
REST stands for Representational State Transfer. REST is web standards based architecture and uses HTTP Protocol for data communication. It revolves around resource where every component is a resource and a resource is accessed by a common interface using HTTP standard methods. REST was first introduced by Roy Fielding in 2000.
Representational State Transfer (REST) is a software architecture style that centers on the transmission of data over HTTP, using only the four basic HTTP verbs. It also eschews the use of any additional wrappers such as a SOAP envelope and the use of any state data.

Overview
Representational State Transfer (REST) is an architectural style first described in a doctoral dissertation by a researcher named Roy Fielding. In RESTful systems, servers expose resources using a URI, and clients access these resources using the four HTTP verbs. As clients receive representations of a resource they are placed in a state. When they access a new resource, typically by following a link, they change, or transition, their state. In order to work, REST assumes that resources are capable of being represented using a pervasive standard grammar.
The World Wide Web is the most ubiquitous example of a system designed on REST principles. Web browsers act as clients accessing resources hosted on Web servers. The resources are represented using HTML or XML grammars that all Web browsers can consume. The browsers can also easily follow the links to new resources.
The advantages of RESTful systems is that they are highly scalable and highly flexible. Because the resources are accessed and manipulated using the four HTTP verbs, the resources are exposed using a URIs, and the resources are represented using standard grammars, clients are not as affected by changes to the servers. Also, RESTful systems can take full advantage of the scalability features of HTTP such as caching and proxies.
HTTP Methods
Following well known HTTP methods are commonly used in REST based architecture.
·         GET - Provides a read only access to a resource.
·         PUT - Used to create a new resource.
·         DELETE - Used to remove a resource.
·         POST - Used to update an existing resource or create a new resource.
·         OPTIONS - Used to get the supported operations on a resource.
Basic Rest Principles
v  RESTful architectures adhere to the following basic principles:
v  Application state and functionality are divided into resources.
v  Resources are addressable using standard URIs that can be used as hypermedia links.
v  All resources use only the four HTTP verbs.
ü  DELETE
ü  GET
ü  POST
ü  PUT
v  All resources provide information using the MIME types supported by HTTP.
v  The protocol is stateless.
v  Responses are cacheable.
v  The protocol is layered.
Resources
Resources are central to REST. A resource is a source of information that can be addressed using a URI. In the early days of the Web, resources were largely static documents. In the modern Web, a resource can be any source of information. For example a Web service can be a resource if it can be accessed using a URI.
RESTful endpoints exchange representations of the resources they address. A representation is a document containing the data provided by the resource. For example, the method of a Web service that provides access to a customer record would be a resource, the copy of the customer record exchanged between the service and the consumer is a representation of the resource.
Rest Best Practices
When designing RESTful Web services it is helpful to keep in mind the following:
ü  Provide a distinct URI for each resource you wish to expose.
For example, if you are building a system that deals with driving records, each record should have a unique URI. If the system also provides information on parking violations and speeding fines, each type of resource should also have a unique base. For example, speeding fines could be accessed through /speedingfines/driverID and parking violations could be accessed through /parkingfines/driverID.
ü  Use nouns in your URIs.
Using nouns highlights the fact that resources are things and not actions. URIs such as /ordering imply an action, whereas /orders implies a thing.
ü  Methods that map to GET should not change any data.
ü  Use links in your responses.
Putting links to other resources in your responses makes it easier for clients to follow a chain of data. For example, if your service returns a collection of resources, it would be easier for a client to access each of the individual resources using the provided links. If links are not included, a client needs to have additional logic to follow the chain to a specific node.
ü  Make your service stateless.
Requiring the client or the service to maintain state information forces a tight coupling between the two. Tight couplings make upgrading and migrating more difficult. Maintaining state can also make recovery from communication errors more difficult.
Designing a Restful Web Service
Regardless of the framework you use to implement a RESTful Web service, there are a number of steps that should be followed:
v  Define the resources the service will expose.
In general, a service will expose one or more resources that are organized as a tree. For example, a driving record service could be organized into three resources:
*      /License/driverID
*      /License/driverID/speedingfines
*      /License/driverID/parkingfines
v  Define what actions you want to be able to perform on each resource.
For example, you may want to be able to update a diver's address or remove a parking ticket from a driver's record.
v  Map the actions to the appropriate HTTP verbs.
Once you have defined the service, you can implement it using Apache CXF.
Sr. No.
HTTP Method
URI
Operation
Operation Type
1
GET
/License/driverID
Get list of License
Read Only
2
GET
/License/driverID/1
Get License with Id 1
Read Only
3
PUT
/License/driverID/2
Insert License with Id 2
Idempotent
4
POST
/License/driverID/2
Update License with Id 2
N/A
5
DELETE
/License/driverID/1
Delete License with Id 1
Idempotent
6
OPTIONS
/License/driverID
List the supported operations in web service
Read Only

Conclusion
In REST architecture, a REST Server simply provides access to resources and REST client accesses and presents the resources. Here each resource is identified by URIs/ global IDs. REST uses various representations to represent a resource like text, JSON and XML. Now a days JSON is the most popular format being used in web services. 

 

0 comments :

Post a Comment

Designed By AMEER BASHA G