Building Restful Web Services using Java

When we think of web services the one obvious question comes to our mind is Soap or Restful? And what are the differences and advantages over one another. In this article we are not going to discuss about the pros an cons of them. When we are developing Restful Web Services we need make sure that we support what is expected out of REST Architectural style. That is –

  • when we are retrieving a resource we should use HTTP GET.
  • when we are creating a resource we should use HTTP POST.
  • when we are deleting a resource we should use HTTP DELETE.
  • when we are updating a resource we should use HTTP PUT.

We will see how we can develop simple restful web services using Java and Spring Boot. We will implement CRUD operations using restful web services.

Software used

  • Java 1.8
  • Spring Boot 1.4.7.RELEASE
  • Eclipse IDE

As mentioned earlier we are using Spring Boot here so all the necessary code plumbing will be done by it and we will directly concentrate on our code sample. We will go through a sample where we will create, update,  get and delete a user from our application system. The pom.xml for reference is as below

In this example we are not using any database but will use a static map which serves like our database during our application run. We have written very simple class to help and interact with this map data. This class will be our repository.

As you can see we have very simple methods  in this class. We will autowire this as our repository and use it in our controllers.

As mentioned earlier we are using Spring Boot for all the underlying configuration. Our main controller is annotated with @RestController that means where every method returns a domain object instead of a view.
Its combined annotation for @Controller and @ResponseBody .

Now we will see one by one each method from our CRUD operation.

Using HTTP GET

In this method we will send complete list of the users available in the system.

You can see we have restricted the listing to only GET method. The method returns list of users as part of ResponseEntity. When we tested the get call in Postman you can see how we get the list of users as part of our response.

Using HTTP POST

As we are testing from Postman, I have restricted the input to JSON only. Here we are using Springs RequestBody annotation that helps us to map/convert the incoming HTTP request to our User Model. Check below the call we did for adding a resource.

If you use any other then POST method type, it will fail.

Using HTTP PUT 

Whenever we are updating any resource we need to use this method type.

Request in Postman

After we updated a resource lets see if it got really changed or not

Using HTTP DELETE

As name suggest, we need to use this whenever we want to remove a resource.

The postman request would be like

and the list after our api is executed successfully

Thats it. Its looks very easy when we combine Restful with Spring Boot.  You can download the code from our Github

Download Code

 

Add a Comment

Your email address will not be published. Required fields are marked *