Java

Building RESTful web services Part – 1

REST vs Microservices:

Before I touch upon RESTful webservice development, let’s understand REST vs Microservices.

Microservices is an architecture style wherein applications are built as a collection of different smaller services rather than one whole app. These smaller services can be independently developed, deployed and updated.

REST over HTTP is the most popular way to implement Microservices. RESTful API focuses more on how to expose these microservices while keeping them decoupled. It makes it easier for these smaller independent applications to communicate with each other. In a nutshell, REST is a medium to build Microservices.

RESTful web services:

RESTful web services are basically REST based architecture web services. REST based architecture is a client – server based architecture where everything is treated as a resource. The REST server will provide access to the resources and a REST client can access and modify the REST resources via a common interface based on the HTTP standard methods.

Following are the HTTP methods:

  • GET defines a reading access of the resource without side-effects. The resource is never changed via a GET request, e.g., the request has no side effects (idempotent).
  • POST request is used to create new resources.
  • DELETE removes the resources.
  • PUrequest is used to update an existing resource.
  • HTTP PATCH is used to make partial update on a resource.

 

Common HTTP response codes:

  • 200 (OK): For any given HTTP GET API, if resource is found on server then it must return HTTP response code 200 (OK) – along with response body which is usually either XML of JSON content (due to their platform independent nature).
  • 404 (NOT FOUND): For any given HTTP GET API, if resource is NOT found on server then it must return HTTP response code 404 (NOT FOUND).
  • 400 (BAD REQUEST): If GET request itself is not correctly formed then server will return HTTP response code 400 (BAD REQUEST).
  • 201 (Created): It indicates that the request has been fulfilled, resulting in the creation of a new resource.
  • 204 (No Content): The server successfully processed the request and is not returning any content as response.
  • 500 (Internal Server Error): A generic error message, given when an unexpected condition was encountered, and no more specific message is suitable.

 

Jersey:

Jersey is the reference implementation for JAX-RS (Java API for RESTful web services), and it contains two major parts.

Core Server: By providing annotations and APIs standardized in JSR 311, you can develop a RESTful Web service in a very intuitive way.

Core Client: The Jersey client API helps you to easily communicate with REST services.

Jersey make it easy to build RESTful Web services utilizing Java and the Java Virtual Machine.

Now let’s see about how to build RESTful web service:

1) In Eclipse, go to File —> New —> Dynamic Web Project.

2) Right click on project —> Configure —> Convert to Maven Project.

The below screenshot shows the structure of project:

3) Open pom.xml file and add below dependencies.

4) Now you need to register Jersey as the servlet dispatcher for REST requests.

Open the file web.xml and modify it to the following:

Once the environment is ready, we can start creating REST APIs.

Let’s see few examples on building and testing REST APIs in the upcoming blog!

About The Author

Leave a Reply

*