How do I read the response header from RestTemplate?

后端 未结 4 855
醉梦人生
醉梦人生 2020-12-05 06:34

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 ge

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 07:26

    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 response = template.exchange(url, HttpMethod.POST, request, String.class);
    
    String resultString = response.getBody();
    HttpHeaders headers = response.getHeaders();
    

提交回复
热议问题