Make REST Calls to The Different Applications Using RestTemplate
Posted By : Kaushlendra Kumar Pal | 27-Sep-2018
1. Overview
In this blog, we will learn about the basic Spring Class RestTemplate which is used for simultaneous client-side HTTP access. By using RestTemplate class we can make Rest calls easily to the different applications. By using RestTemplate we can make calls to any type of request but in this blog, we will learn about only two most important types of Rest calls for HTTP request of type GET and POST by using different types of RestTemplate APIs.
2. Use GET Request to Retrieve Resources
2.1 Get Plain JSON
We can make HTTP GET request by simply using getForEntity() API. Let's start with a simple example of GET Request using getForEntity() API.
RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl = "http://localhost:8080/spring-rest/foos";
ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
You can see that we can access full details of the HTTP Response. By using that we can check the status code to make sure that the operation is successful, or can work with the actual body of the HTTP Response.
Let's see with an example:
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(response.getBody());
JsonNode name = root.path("name");
assertThat(name.asText(), notNullValue());
Here we can work with the R
2.2 Get POJO Instead of JSON
RestTemplate also provides the feature to map the response directly to the Resource DTO or POJO class by using getForObject() API.
For Example:
public class Foo implements Serializable {
private long id;
private String name;
//standard getters and setters
}
Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class);
assertThat(foo.getName(), notNullValue());
assertThat(foo.getId(), is(1L));
3. Use POST Request to Create a Resource
To create a new Resource by using RestTemplate we can use postForLocation() which returns the URI of the newly created Resource, postForObject() or postForEntity() which returns the Resource itself APIs.
3.1 The postForObject API
ClientHttpRequestFactory requestFactory = getClientHttpRequestFactory();
RestTemplate restTemplate = new RestTemplate(requestFactory);
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
Foo foo = restTemplate.postForObject(fooResourceUrl, request, Foo.class);
assertThat(foo, notNullValue());
assertThat(foo.getName(), is("bar"));
3.2 The postForLocation API
Similarly, if we want an operation that instead of returning the full Resource just return the Location of that newly created Resource. Let's take an example for that:
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
URI location = restTemplate.postForLocation(fooResourceUrl, request);
assertThat(location, notNullValue());
3.3 The exchange API
Finally, it is the most useful API of RestTemplate let's take an example with the more Generic exchange API to do a POST request:
RestTemplate restTemplate = new RestTemplate();
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
ResponseEntity<Foo> response = restTemplate.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
Foo foo = response.getBody();
assertThat(foo, notNullValue());
assertThat(foo.getName(), is("bar"));
I hope you guys learned something useful by me. ThankYou!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Kaushlendra Kumar Pal
Kaushlendra is an experienced Java Developer with comprehensive mastery of all Java packages. Able to Write well designed, testable, efficient code as per the requirements and handle different types of issues as well as functions.