Eric Milas Eric Milas - 1 year ago 94
Java Question

How do I read the response header from RestTemplate?

I am posting information to a web service using RestTemplate.postForObject. Besides the result string I need the information in the response header. Is there any way to get this?

RestTemplate template = new RestTemplate();
String result = template.postForObject(url, request, String.class);

Answer Source

Ok, I finally figured it out. The exchange method is exactly what i need. It returns an HttpEntity which contains the full headers.

RestTemplate template = new RestTemplate();
HttpEntity<String> response =, HttpMethod.POST, request, String.class);

String resultString = response.getBody();
HttpHeaders headers = response.getHeaders();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download