A simple way to log Spring RestTemplate requests and responses.
- Spring web 4+
- Slf4j 1.7.7+
This project includes a new RestTemplate interceptor (RequestLoggerInterceptor) that logs the request and response. For this, you have to add this interceptor to your RestTemplate instance. Also, you will need to change the default request factory for a BufferingClientHttpRequestFactory, this is necessary to log de response body.
Add the dependency to pom.xml
<dependency>
<groupId>ar.com.onready</groupId>
<artifactId>spring-resttemplate-logger</artifactId>
<version>1.0.0</version>
</dependency>
Then, configure your RestTemplate instance:
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
if (CollectionUtils.isEmpty(interceptors)) {
interceptors = new ArrayList<ClientHttpRequestInterceptor>(1);
}
interceptors.add(new RequestLoggerInterceptor());
restTemplate.setInterceptors(interceptors);
In order to view the logs, the package ar.com.onready.springrequestlogger
must be log in debug mode.