

Spring boot Rest Template is used to call rest service, getForEntity will return ResponseEntity which contains response + response metadata like header information,url ect. ResponseEntity contains response header, Here is an example of Spring boot Resttemplate get headers. Here is spring boot rest template example.
Example: Spring boot Resttemplate get headers
@Autowired
private RestTemplateBuilder restTemplate;
public Employee getHeaders() {
ResponseEntity responseEntity = restTemplate.build()
.getForEntity("http://localhost:8080/getEmployee/{id}", Employee.class, 2);
responseEntity.getHeaders().entrySet().forEach((k) -> {
System.out.println("Key is:"+ k.getKey());
System.out.println("Values are:"+k.getValue().stream().collect(Collectors.joining()));
});
return (Employee) responseEntity.getBody();
}Output:
Key is:Content-Type Values are:application/json;charset=UTF-8 Key is:Transfer-Encoding Values are:chunked Key is:Date Values are:Mon, 20 Nov 2017 17:09:16 GMT
References:
Spring boot rest template document
Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.
