Spring Boot RestTemplate GET Example

In this, Spring Boot RestTemplate GET request example, learn to use RestTemplate to invoke REST GET API verify api response status code and response entity body.

To create the rest apis, use the sourcecode provided in spring boot 2 rest api example.

1. Maven dependencies

Make sure to have spring-boot-starter-web dependency in the project.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<scope>test</scope>
</dependency>

2. Spring Boot RestTemplate GET API Examples

In the given example, I will first write the rest API code and then write the unit-test which invokes the rest API and verifies API response.

2.1. HTTP GET Request

A simple API returning the list of employees in a wrapper object Employees.

@GetMapping(value = "/employees", 
	produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public EmployeeListVO getAllEmployees(
	@RequestHeader(name = "X-COM-PERSIST", required = true) String headerPersist,
        @RequestHeader(name = "X-COM-LOCATION", defaultValue = "ASIA") String headerLocation) 
{
	LOGGER.info("Header X-COM-PERSIST :: " + headerPersist);
	LOGGER.info("Header X-COM-LOCATION :: " + headerLocation);
	
    EmployeeListVO employees = getEmployeeList();
    return employees;
}

2.2. RestTemplate example to consume the GET API

In the given example, we are using RestTemplate to invoke the above API and verify the API, HTTP response code as well as the response body.

RestTemplate restTemplate = new RestTemplate();
    
final String baseUrl = "http://localhost:" + randomServerPort + "/employees";
URI uri = new URI(baseUrl);

ResponseEntity<String> result = restTemplate.getForEntity(uri, String.class);
    
//Verify request succeed
Assert.assertEquals(200, result.getStatusCodeValue());
Assert.assertEquals(true, result.getBody().contains("employeeList"));

2.3. RestTemplate example to send request headers

In this example, we are sending two headers. X-COM-PERSIST header is mandatory and X-COM-LOCATION is optional.

The example invokes GET API with mandatory headers and verifies the API response code as well as the response body.

Note: RestTemplate getForEntity() method does not support request headers. Please use exchange() method if headers are necessary.

The request is executed successfully
RestTemplate restTemplate = new RestTemplate();
    
final String baseUrl = "http://localhost:"+randomServerPort+"/employees/";
URI uri = new URI(baseUrl);
    
HttpHeaders headers = new HttpHeaders();
headers.set("X-COM-PERSIST", "true");  
headers.set("X-COM-LOCATION", "USA");

HttpEntity<Employee> requestEntity = new HttpEntity<>(null, headers);

ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, String.class);
    
//Verify request succeed
Assert.assertEquals(200, result.getStatusCodeValue());
Assert.assertEquals(true, result.getBody().contains("employeeList"));
The request is faled with error code

To produce an error scenario, let’s do not add the mandatory header in the request entity.

RestTemplate restTemplate = new RestTemplate();
    
final String baseUrl = "http://localhost:"+randomServerPort+"/employees/";
URI uri = new URI(baseUrl);
    
HttpHeaders headers = new HttpHeaders();
headers.set("X-COM-LOCATION", "USA");

HttpEntity<Employee> requestEntity = new HttpEntity<>(null, headers);

try 
{
    restTemplate.exchange(uri, HttpMethod.GET, requestEntity, String.class);
    Assert.fail();
}
catch(HttpClientErrorException ex) 
{
    //Verify bad request and missing header
    Assert.assertEquals(400, ex.getRawStatusCode());
    Assert.assertEquals(true, ex.getResponseBodyAsString().contains("Missing request header"));
}

Let me know if you have any queries in this spring boot RestTemplate get API example.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Spring Boot RestTemplate GET Example”

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.