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?

Leave a Reply

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