March 23, 2020
Estimated Post Reading Time ~

Overview on Building Restful Web Services Part – 1



REST vs Microservices:
Before I touch upon RESTful web service 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 reading access to 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.
· PUT request is used to update an existing resource.
· HTTP PATCH is used to make a partial update on a resource.

Common HTTP response codes:
· 200 (OK): For any given HTTP GET API, if the resource is found on the server then it must return HTTP response code 200 (OK) – along with response body which is usually either XML or JSON content (due to their platform-independent nature).
· 404 (NOT FOUND): For any given HTTP GET API, if the resource is NOT found on the server then it must return HTTP response code 404 (NOT FOUND).
· 400 (BAD REQUEST): If the GET request itself is not correctly formed then the 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 a 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 makes 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 the project —> Configure —> Convert to Maven Project.
The below screenshot shows the structure of the project:


3) Open the pom.xml file and add below dependencies.
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${jaxrs.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.21</version>
</dependency>

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:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MyProject</display-name>
<servlet>
<servlet-name>MyProject REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under MyProject package -->
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
com.wordnik.swagger.jersey.listing.ApiListingResourceJSON,
com.wordnik.swagger.jersey.listing.JerseyApiDeclarationProvider,
com.wordnik.swagger.jersey.listing.JerseyResourceListingProvider,
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.wordnik.swagger.jaxrs.json,com.cmsbridge.api
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyProject REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

Once the environment is ready, we can start creating REST APIs.
Let’s see a few examples of building and testing REST APIs in the upcoming blog!

Source:
https://labs.tadigital.com/index.php/2018/07/13/building-restful-web-services-part-1/



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.