Auditing Request and Response Spring RestTemplate

Prashant Gangwar
2 min readApr 27, 2022
Interceptor to log request/response for RestTemplate

In this tutorial we’re going to learn how can we implement audit of request and response data of services calls exchanged between two servers. We can use interceptor to audit before and after the remote call.

Following are the steps to add interceptor.

1- Create an Interceptor
2- Configure RestTemplate

Create an Interceptor

You’ll need to create one interceptor by implementing the interface ClientHttpRequestInterceptor and implement the intercept method. In the intercept method we will call execute method on ClientHttpRequestExecution to continue the service call and later we will call our AuditService to audit the request/response.

@Component
public class AuditInterceptor implements ClientHttpRequestInterceptor {
@Autowired
public AuditService auditService;
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
ClientHttpResponse response = execution.execute(request, body);
auditService.auditRequest(request, response, body);
return response;}}

We’ve Created one service class which will audit the request/response data from service call, in this class you use any mechanism to audit the data. In our class we’ve just logged all data to log file named as audit.log

@Service
public class AuditService {
private final Logger log = LoggerFactory.getLogger(AuditService.class);public void auditRequest(HttpRequest request, ClientHttpResponse response, byte[] body) throws IOException {log.debug("========Audit started for request and response from rest Template calls======");
log.debug("Service URI : {}", request.getURI());
log.debug("Request Method : {}", request.getMethod());
log.debug("Headers: {}", request.getHeaders());
log.debug("Request body: {}", new String(body, "UTF-8"));
log.debug("Response Status code: {}", response.getStatusCode());
log.debug("Response Status text: {}", response.getStatusText());
log.debug("Response body: {}", StreamUtils.copyToString(response.getBody(),Charset.defaultCharset()));
log.debug("========End of request and response audit======");}}

Configure RestTemplate

In this step, we need to register the interceptor to RestTemplate that we created. You can add as many interceptors and spring will chain them together at run time.

while configuring interceptor we need to ensure that we’ve used correct Request Factory to RestTemplate. Otherwise there would be issue on getting the response body, which you can get only once, so if you get that while auditing the response then you can not get it in actual service from where it is called.

@Configuration
public class ResttemplateConfig {
@Autowired
public AuditInterceptor auditInterceptor;
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
ClientHttpRequestFactory requestFactory = new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory());
restTemplate.setRequestFactory(requestFactory);
restTemplate.getInterceptors().add(auditInterceptor);
return restTemplate;}
}

You can get the complete source code from GitHub by this link.

If you have any questions, you can drop comments on the article.

If this article helps you, Please do like and follow me for more article!!!

Happy Coding and Keep Learning!!!

--

--

Prashant Gangwar

Technical Lead focused on backend services, micro services, enthusiastic for new tech skills