How do I read the response header from RestTemplate?

后端 未结 4 844
醉梦人生
醉梦人生 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条回答
  •  独厮守ぢ
    2020-12-05 07:12

    Best thing to do whould be to use the execute method and pass in a ResponseExtractor which will have access to the headers.

    private static class StringFromHeadersExtractor implements ResponseExtractor {
    
        public String extractData(ClientHttpResponse response) throws   
        {
            return doSomthingWithHeader(response.getHeaders());
        }
    }
    

    Another option (less clean) is to extend RestTemplate and override the call to doExecute and add any special header handling logic there.

提交回复
热议问题