Introduction

RESTful

Introduction


1. What is REST Service Architecture Style

REST (REpresentational State Transfer) is an architectural style used for interaction between hypermedia system components.

The central abstraction in REST is a resource. A resource is accessed by clients using HTTP standard methods (GET, POST, PUT, DELETE).

Principles of REST architecture:

  • Client–server
  • Stateless
  • Cacheable
  • Layered system
  • Code on demand (optional)
  • Uniform interface

2. What is RESTful Web Services

Web services based on REST Architecture are known as RESTful web services. 

RESTful web service should have such properties:

  • Resources
  • Representation
  • Uniform Interface
  • State

 Let's discuss each of them.

2.1. Resources

Resource is the central point of Restful web services. A resource can consist of other resources. Each resource should have at least one URI. The URI should not contain any information about the operation. This gives the possibility to call the same URI with different HTTP verbs. Let's say there are a database with exams and we want to give information about them. A resource for this purpose can be like:

http://ec.com/exams/1

This resource can be accessed with GET, DELETE or PUT verbs.

Best practices for creating URIs:

  • Use plural nouns in the name of resources. For example http://ec.com/exams.
  • Don't use verbs for resource names. For example, a RESTful service should not have the URIs http://ec.com/GetExam?id=1 or http://ec.com/DeleteExam?id=1. Use the URI http://ec.com/exams/1 with HTTP verbs  GET and DELETE.
  • Use query parameters only for providing parameter values to a process. For example: http://ec.com/exams/1?format=json&encoding=UTF8. Avoid using  query parameters in this way: http://ec.com/exams?id=1.

 2.2. Representation

 Such as JSON, HTML, XML.

2.3. Uniform Interface

 Restful web service should have a uniform interface. HTTP provides methods, called verbs, for this. The more important verbs are:

HTTP verb  Operation Quality
POST Create a new resource from the request data N/A
GET Read a resource Safe
PUT Update a resource from the request data Idempotent 
DELETE Delete a resource Idempotent
OPTIONS List the allowed operations on a resource Safe
HEAD Return only the response headers and no response body  Safe

Safe operations does not have any effect on the value of the resource. Idempotent operations return the same result no matter how many times they are performed.

2.4. State

A resource can have a state. The resource's state is defined by the set of data the server stores. If to speak about the previous example with the exams database - the state is changed when a new exam is added.

Don't mix it with stateless principle, which is connected with application state.

3. What is the Difference between REST and SOAP Web Service

  • SOAP is a set of messaging protocol and standards, and REST is a style of software architecture for distributed hypermedia systems. SOAP uses WSDL protocol, which fully describes web service. REST has a WADL protocol, which isn't used and in fact only adds complexity.
  • A response is transferred to the client as the body of an HTTP response message in REST. And a SOAP message is encapsulated as the body of a transport message.
  • SOAP uses HTTP as a transport protocol and REST is based on it. This means that all mechanisms of HTTP protocol can be used with REST.
  • A response in REST can be in different formats (for example JSON, XML, HTML) and SOAP is tied to XML.
  • SOAP works with operations and REST with resources.

See also How to build RESTful Web Services using Jersey, JAX-RS - OCEJWSD.

Sources: Dr.Dobb's - RESTful Web Services: A Tutorial, Create RESTful Web services with Java technology.



0 comments
Leave your comment: