Skip to content

Instantly share code, notes, and snippets.

@agrawald
Created August 28, 2019 00:41
Show Gist options
  • Save agrawald/538cbc342d77f04500c218053a375111 to your computer and use it in GitHub Desktop.
Save agrawald/538cbc342d77f04500c218053a375111 to your computer and use it in GitHub Desktop.
Spring-boot: RestTemplate: Reading Response Body multiple times
@Bean
public RestTemplate restTemplate(final LauHeaderInterceptor lauHeaderInterceptor) {
final RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(Collections.singletonList(new MappingJackson2HttpMessageConverter()));
restTemplate.setErrorHandler(new ResponseErrorHandler() {
@Override
public boolean hasError(final ClientHttpResponse response) throws IOException {
return false;
}
@Override
public void handleError(final ClientHttpResponse response) throws IOException {
log.error("Error while fetching the response: {}", response);
}
});
restTemplate.setInterceptors(Collections.singletonList(lauHeaderInterceptor));
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); //this is the magic
return restTemplate;
}
@parameshjava
Copy link

Will you please provide implementation for BufferingClientHttpRequestFactory?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment