Home » RESTful Webservices » RESTful Web Services - Introduction

RESTful Web Services - Introduction

What is REST ?

REST stands for Representational State Transfer.REST is used to build Web services that are lightweight, maintainable, and scalable in nature. A service which is built on the REST architecture is called a RESTful service.REST uses HTTP Protocol for data communication.

REST was introduced and defined by Roy Fielding in 2000. The idea is rather than using complex mechanism such as CORBA,RPC or SOAP to connect applications, simple HTTP protocol is used.

RESTful application use HTTP requests to send data(Create/Update),read data and delete data. Thus REST uses HTTP for all four CRUD operations.

REST is a way to access resources which lie on a particular server. For example, you could have a server that could be hosting important documents or pictures or videos. All of these are an example of resources. If a client, say a web browser needs any of these resources, it has to send a request to the server to access these resources. Now REST defines a way on how these resources can be accessed.

Key elements of a RESTful implementation

The key elements of a RESTful implementation are as follows:

  1. Resources – Resources is the key element of Restful Services. Let us assume that a web application on a server has records of millions of employees. Let's assume the URL of the web application is http://example.javawebtutor.com Now in order to access an employee record resource via REST, one can issue the command http://example.javawebtutor.com/employee/101 - This command tells the web server to provide the details of the employee whose employee ID is 101.
  2. Request Verbs - These describe what you want to do with the resource. A browser issues a GET verb to instruct the endpoint it wants to get data. However there are many other verbs available including things like POST, PUT, and DELETE. So in the case of the example http://example.javawebtutor.com/employee/101 , the web browser is actually issuing a GET Verb because it wants to get the details of the employee record.
  3. Request Headers – These are additional instructions sent with the request. These might define the type of response required or the authorization details.
  4. Request Body - Data is sent with the request. Data is normally sent in the request when a POST request is made to the REST web service. In a POST call, the client actually tells the web service that it wants to add a resource to the server. Hence, the request body would have the details of the resource which is required to be added to the server.
  5. Response Body – This is the main body of the response. So in our example, if we were to query the web server via the request http://example.javawebtutor.com/employee/101 , the web server might return an XML document with all the details of the employee in the Response Body.
  6. Response Status codes – These codes are the general codes which are returned along with the response from the web server. An example is the code 200 which is normally returned if there is no error when returning a response to the client.

RESTful Resources

In REST based architecture everything is a resource. In a REST architecture we typically have a REST server which provides access to the resources and a REST client which accesses and modify REST resources. In REST, the web services are viewed as resources and can be identified by their URLs(URI). REST allows that resources have different representations, e.g., text, XML, JSON etc. The REST client can ask for a specific representation via the HTTP protocol . Now a days JSON is the most popular format being used in web services.

Web service clients that want to use these resources and access a particular representation will need to use a globally defined set of remote methods that describe the action to be performed on the resource.Client applications use HTTP method such as GET, POST, PUT or DELETE to manipulate the resource or collection of resources.

These are some examples of REST resources:

  • Share price of Stock Market.
  • A search result for a particular Item in e-commerce website.
  • Account detail of particular customer of a bank.

Representation

The representation of resources is what is sent back between clients and servers.A representation can take various forms, such as an image, a text file, or an XML stream or a JSON stream.You can use any format for representing the resources, as REST does not put a restriction on the format of a representation.

JSON representation of a resource.

{
    "ID": "101",
    "Name": "Mukesh",
    "Email": "admin@javawebtutor.com",
    "Country": "India"
}

XML representation of a resource.

<Person>
<ID>101</ID>
<Name>Mukesh</Name>
<Email>admin@javawebtutor.com</Email>
<Country>India</Country>
</Person>

HTTP Methods:

Following HTTP methods are commonly used in REST based architecture.we can map CRUD actions to the HTTP methods POST, GET, PUT, and DELETE as follows:

  • GET - Retrieve Data
  • POST- Create/Insert Data
  • PUT- Update Data
  • DELETE- Delete Data

Let's assume that we have a RESTful web service is defined at the location. http://example.javawebtutor.com/employee . When the client makes any request to this web service, it can specify any of the normal HTTP verbs of GET, POST, DELETE and PUT. Below is what would happen If the respective verbs were sent by the client.

  • POST – This would be used to create a new employee using the RESTful web service.
  • GET - This would be used to get a list of all employee using the RESTful web service.
  • PUT - This would be used to update all employee using the RESTful web service.
  • DELETE - This would be used to delete all employee using the RESTful web service.

Let's take a look from a perspective of just a single record. Let's say there was an employee record with the employee number of 1.

The following actions would have their respective meanings.

  • POST – This would not be applicable since we are fetching data of employee 1 which is already created.
  • GET - This would be used to get the details of the employee with Employee no as 1 using the RESTful web service.
  • PUT - This would be used to update the details of the employee with Employee no as 1 using the RESTful web service.
  • DELETE - This is used to delete the details of the employee with Employee no as 1.

  • GET/RETRIEVE

    The method GET is used to RETRIEVE resources.

    For example consider a web service managing students in some classroom with a location of http://localhost:8080/StudentManagement/rest/StudentService/. For thisservice, we assume an XML representation of a student to look as follows:

    XML representation of a student

    <student>
    <name>Ravi</name>
    <age>10</age>
    <link>/students/Ravi</link>
    </student>
    

    XML representation of list of students

    <students>
    <student>
    <name>Ravi</name>
    <age>10</age>
    <link>/students/Ravi</link>
    </student>
    <student>
    <name>Amit</name>
    <age>12</age>
    <link>/students/Amit</link>
    </student>
    <students>
    

    With the representations defined above, we can use URIs of the form http://localhost:8080/StudentManagement/rest/StudentService/students to access a list of students, and http://localhost:8080/StudentManagement/rest/StudentService/{name} to access a specific student that has the unique identifier of value name.

    For example,if we wanted the record for a student with the name Ravi, we need to make a request to the URI http://localhost:8080/StudentManagement/rest/StudentService/Ravi. The response from the service will contain the representation of student with the name Ravi and looks something like

    <student>
    <name>Ravi</name>
    <age>10</age>
    <link>/students/Ravi</link>
    </student>
    

    At the same time we can access a list of students through the URI http://localhost:8080/StudentManagement/rest/StudentService/students The response from the service will contain the representation of all students and may look like (assuming there are three students available):

    <students>
    <student>
    <name>Ravi</name>
    <age>10</age>
    <link>/students/Ravi</link>
    </student>
    <student>
    <name>Amit</name>
    <age>12</age>
    <link>/students/Amit</link>
    </student>
    </student>
    <student>
    <name>Vicky</name>
    <age>8</age>
    <link>/students/Vicky</link>
    </student>
    <students>
    

    POST/CREATE

    Post method is used to create a new resource or Updating an existing resources. For example the URI to create a new student in our list is http://localhost:8080/StudentManagement/rest/StudentService/Ravi. The method type for the request is set by the client.


    PUT/UPDATE

    PUT method is used for Create/Updating a REST resource.


    DELETE

    The method DELETE removes the resources. The operations are idempotent. They can get repeated without leading to different results.


    RESTFul web services

    A RESTFul web services are based on HTTP methods and the concept of REST. A RESTFul web service typically defines the base URI for the services, the supported MIME-types (XML, text, JSON, user-defined, ...) and the set of operations (POST, GET, PUT, DELETE) which are supported.

    Java defines REST support via the Java Specification Request (JSR) 311. This specification is called JAX-RS (The Java API for RESTful Web Services). JAX-RS uses annotations to define the REST relevance of Java classes.

    Whats Next

    You can start your RESTFulnwebservices Journey by learning about the basics and example program.

    Next Article

comments powered by Disqus