Web Service works on the principle of the client-server model. The basic idea behind web services is the communication between interoperating software running on different technologies. Web service uses XML to pass structured messages between the client and server. It is a standardized medium to send messages and establish communication between the client and server applications on the World Wide Web.

Web Services

RESTful Web Services – Introduction

  • Rest is one of the popular web services in today’s world.
  • REST was first introduced by Roy Fielding in 2000.
  • REST stands for Representational State Transfer.
  • REST is web standards-based architecture and uses HTTP Protocol which is the basic web protocol.
  • REST is used to build Web services that are lightweight, maintainable, and scalable in nature.
  • Web services based on REST Architecture are known as RESTful web services.

Key Elements of RESTful Web Service

1. Resources

REST architecture treats every content as a resource. These resources can be Text Files, Html Pages, Images or Dynamic Business Data.

Let’s assume the URL to access an employee record resource via REST is as shown below:

Example: – http://localhost:8080/RestDemo/employees/employee/101

Here, the URL tells the web server to provide the details of the employee whose employee number is 101.

2. Request Verbs

Request Verbs describe what action you want to do with the resource. RESTful APIs enable you to develop any kind of web application having all possible CRUD (create, retrieve, update, delete) operations by using the request verbs.

GET – It is used to read from a REST resource.
POST – POST verb is used to create a new REST resource.
PUT – It is used to update an already existing REST resource.
DELETE – To delete a REST resource.

3. Request Headers

These are additional instructions sent along with the request. Request headers define the type of response, response code, and the authorization details.

4. Request Body

Request Body is used to send the data received from the client to the server while making a POST or PUT request.

5. Response Body

Response Body contains the data sent by a web server in the response of the client request.

Example: – http://localhost:8080/RestDemo/employees/employee/101

The web server might return an XML or JSON with all the details of the employee in the Response Body.

6. Response Status codes

These codes are the general codes that 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.

Advantages of REST Web Services

  • Supports multiple technologies for data transfer such as text, XML, json, image, etc.
  • No contract defined between server and client, so loosely coupled implementation.
  • Easy to create and test the REST web services, require less time to develop.
  • REST is a lightweight protocol.

REST Web Services API

  • JAX-RS is the Java API for creating REST web services.
  • JAX-RS uses annotations to simplify the development and deployment of web services.
  • JAX-RS API has two different implementations for REST services.
    1. Jersey: In case of Jersey implementation, we need to configure its servlet in web.xml and add required dependencies.
    2. RESTEasy: RESTEasy is the JBoss project that provides JAX-RS implementation.

JAX-RS API Annotations

  • @Path: used to specify the relative path of the resource class and methods.
  • @GET: used to read from a rest resource.
  • @POST: used to create a new resource.
  • @PUT: used to update an already existing resource or to add a new resource.
  • @DELETE: used to delete a rest resource.
  • @Produces: annotation used to specify the MIME media types or representations a resource can produce and send back to the client.
  • @Consumes: annotation specifies the list of media types consumed by a particular method or class.
  • @PathParam: annotation used to bind URL parameters to method arguments.