Monday, March 20, 2017

Develop Your First REST Web Service with Java Spring

Hi All,

Let's develop our first REST web service. There are various libraries and methods you can follow to develop a REST API. But here we are going to use Spring framework to get this done in a much more easier manner. Here I am using IntelliJ IDEA 2016 to develop my application.

First let's go and create a maven project in Idea. Here we are not going to select a archetype. We will just use the existing template.

Let's update the pom.xml. We need to specify our spring dependencies and also the tomcat plugin to run our service locally. We will also need to add Jackson to do JSON mapping for us. Once you have update the pom file, it should should look like below. 


Once the dependencies are added to the project, you can check that by going into the project structure. 

Then we need to specify the WebApp folder where the necessary web resources are present. It's important that we adhere to the given structure, otherwise during the project build and packaging, we need to edit the pom to recognize where our resources exactly resides in. Project structure should look like below. 

Now let's create a package and add our REST controller class inside the package. REST controller is acting as an endpoint to the requests. It will get the requests as GET, POST and etc. according to the requests it will reply with the necessary output. To specify this class as a controller we need to use the '@RestController' annotation. Then we can optionally use the '@RequestMapping' to specify the path to our controller from the root. In order to include header for CORS( Cross-Origin Resource Sharing ) we need to use '@CrossOrigin' annotation. To read more about CORS you can refer to this link


Now we have setup our rest controller. Let's add some REST methods to get some work done with our controller. There are several rest methods like GET, POST, PUT, PATCH, DELETE. You can read about the REST methods here

Before adding the methods, lets create a simple Person class and a list of people inside our controller, just to test both GET and POST methods.


Now we can go and create our methods. Following is the complete code for the controller class.


We also need to add the servlet.xml and web.xml inside WEB-INF folder. 


Now let's run our program first and then look into the methods and what we have done in each. 

In order to run the program we can create new debug configuration in IntelliJ as following. Then you can click on the debug button to debug the program. 

In order to check the results it's good to install the Postman plugin for chrome. 

Now lets check our results while referring to the methods. 

The first method is a GET method that creates a Person and returns the object as the response. The person object is converted to a JSON object automatically with Jackson Data Binding. The method is just a simple GET method without any arguments passed into it. You can access the web service from the following URI. 

http://localhost:8080/JavaSpringRESTDemo/learning/newperson

If you check the result through Postman it will be as following. 


Second addPerson method is a POST method, that accepts a Person as a JSON object and adds the Person to a list. Here the Person object is again sent as a JSON object and it is mapped to a POJO by Jackson data binding. You can send the POST request as following. 

Once you send the request, response will be the existing list with the added Person as a JSON object. 

Now let's use a URL parameter to get the person, when the user ID is specified. URL parameter is specified by the annotation @RequestParam(value = "id"). So we have to specify the URL as following and get the output object. 

There is another way we can specify a parameter in the URL itself. It's by using a Path Variable. The final method is developed to accept a path variable containing a user ID. You can use the following URL to get the person with ID 15. 

So that's should be it for now. Hope to publish more with Spring Related stuff. 

Hope that helps. 
Thank you.

Sunday, March 19, 2017

Communicate Within Wars Inside Same Container

Hi All,

There are certain scenarios when you want to communicate within the wars inside the same container without using the network. For an example, you need to avoid any network delay that would cause either by using web-services,RMI or HTTP.

So what I'm going to show you is one working solution to achieve the above problem. What we can do is introduce a common library that is a dependent to both the services. Through the intermediate library we can continue the communication among the war files.

For this example, I'm using a service called, Front Service to accept the user request. Then a second service called 'Ground Service' that contains a method need to be called by the Front Service. The communication between the services are done with a JAR named 'Common-Lib'.

Common Lib has an interface which is similar to the Ground Service. It contains the method definitions of the Ground Service.



Common Lib also has a class that gets and sets the Service Instances registered to the jar.



Then let's develop the Front Service. This service is a simple Spring service, that gets a message from the Ground Service.



Finally we need to implement the Ground Service.
Here we have the Ground Service class and the helper classes to map a service instance to the Service Handler. There is a class named GroundServiceConfig, that sets an instance of the IGroundService interface to the Service Handler. IGroundService is implemented in the GroundServiceAdapter class.







Now we have the two services and the common library defined. It's important to note that although common-lib is a dependent of both the services, we do not bundle them with the service. Therefore in the POM.xml, we specify the 'common-lib' dependency as provided.



Therefore we need to deploy the common-lib in the 'lib' inside tomcat installation.

What happens here is that, when the Ground Service is initializing, it will assign a new instance of the IGroundService in the Service Handler. This instance can then be used to call the service methods from the other Services that uses 'Common-Lib' as a dependency.

Now let's deploy the two services inside the web-apps and put the common-lib inside the 'lib'

Let's go to the Front Service URL and check the result. As you can see we have got the message successfully from the GroundService.

The complete projects and code can be found here.

Hope that helps.
Thank You.