Saturday, July 1, 2017

Consuming JSON using RESTful WebService on Spring Framework

hello everyone,

Today I am going to show you how to consume JSON using RESTFUL WebService on Spring.


  1. Looking from Spring REST client , the RestTemplate is the core class for client-side access to Spring RESTful web services. It goes over HTTP and it basically communicates to HTTP server using RESTful constraints. It is very similar to other template classes in the Spring like HibernateTemplate and JdbcTemplate etc. 
  2. In Spring, RestTemplate provides higher level implementation of corresponding HTTP methods such as GET, POST, PUT, DELETE, HEAD etc. It provides the methods to communicate by using these HTTP methods with URI template, URI param, request object and response type as arguments.

In this example, we will see how to consume JSON response. We have seen Spring RESTful web services crud example. Earlier I have used postman REST UI based client to demonstrate all HTTP methods such as get, post, delete and put. But here we are going to consume Restful web services via RestTemplate of Spring REST


RestTemplateMethods


RestTemplate provides higher level methods that correspond to each of the six main HTTP methods that make invoking many RESTful services. In the following list has methods provided by Spring RestTemplate for each http methods.



If you distinguish per name of these methods in RestTemplate class, it shows which HTTP method they implement internally and second part of the method name indicates what is return by these methods. For example, getForObject() will perform a HTTP GET action on the server and later convert the HTTP response into an object of given type and  then return it to the CLIENT. The other method postForLocation() will do a HTTP POST action on the server - converting the given object into a HTTP request, and returns the response HTTP Location header where the newly created object can be found. 

Let's see these RestTemplate's methods with examples.


 Here I am using Spring Restful web services CRUD JSON Example: 

In this example, we are using @GetMapping, @PostMapping, @PutMapping and @DeleteMapping annotations, these annotations are introduced as of Spring 4.3 version in parallel of @RequestMapping annotation with Http Methods as below.



HTTP GET Method Example
For API GET method call we can use either getForObject() or getForEntity() method of RestTemplate.

Spring REST API GET CODE:




We can define same above API call using this way below:




This call binds to convert on JSON media type only with the @RequestMapping annotation.

NOTE: If you want flexible return media type, let's say JSON or XML . These will be based on the message converter available on the classpath(application). Then it's recommended to not define producers property request mapping annotation. 

Spring RestTemplate get Method :





Output on console:
After running above code, you will get below output:
Account Detail for given AccountId : Account [accountId=1, name=Thiago Leoncio, city=BeloHorizonte, balance=212.45]

HTTP POST Method Example

For Http POST method calling we can use either postForObject() or postForLocation() method of RestTemplate.

Spring Rest API Code post method:



Spring RestTemplate post Method call:




Output on console:
After running above code, you will get below output:
Added account: Account [accountId=3, name=Arthur Jordan, city=BeloHorizonte, balance=312.45]

HTTP PUT Method Example

For Http PUT method calling we can use put() method of RestTemplate.

Spring Rest API Code put method:





Spring RestTemplate put Method call:





Output on console:
After running above code, you will get below output:

Added account: Account [accountId=1, name=Arthur Jordan, city=BeloHorizonte, balance=312.45]
Updated Account: Account [accountId=1, name=Love Rocha, city=BeloHorizonte, balance=123.45]

HTTP DELETE Method Example

For Http DELETE method calling we can use delete() method of RestTemplate.

Spring Rest API Code delete method:






Spring RestTemplate delete Method call:






Output on console:
After running above code, you will get below output:
Total Accounts before DELETE call: 
{accountId=1, name=Love Rocha, city=BeloHorizonte, balance=123.45}
{accountId=2, name=Love Rocha, city=BeloHorizonte, balance=222.45}
{accountId=3, name=Arthur Jordan, city=BeloHorizonte, balance=522.45}
Total Accounts after DELETE call: 
{accountId=1, name=Love Rocha, city=BeloHorizonte, balance=123.45}
{accountId=3, name=Arthur Jordan, city=BeloHorizonte, balance=522.45}



Reference: Spring documentation - Consuming REST  https://spring.io/guides/gs/consuming-rest/


I hope it helps you.

Happy coding,
Thiago Leoncio.