Learn to use
ClientHttpRequestInterceptor
with Spring RestTemplate
to log request and response headers and body in Spring AOP style.Interceptor Usages
We can use this interceptor for many useful tasks.
- To add custom header to the response
- To log HTTP request and response
- To deny certain requests etc.
1) Interceptor to log request and response
Below given
RequestResponseLoggingInterceptor
class implements ClientHttpRequestInterceptor
interface. It implements intercept()
method. In this method, we are logging the request and response details sent from RestTemplate
.import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.util.StreamUtils; import java.io.IOException; import java.nio.charset.Charset; public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor { private final Logger log = LoggerFactory.getLogger( this .getClass()); @Override public ClientHttpResponse intercept(HttpRequest request, byte [] body, ClientHttpRequestExecution execution) throws IOException { logRequest(request, body); ClientHttpResponse response = execution.execute(request, body); logResponse(response); //Add optional additional headers response.getHeaders().add( "headerName" , "VALUE" ); return response; } private void logRequest(HttpRequest request, byte [] body) throws IOException { if (log.isDebugEnabled()) { log.debug( "===========================request begin================================================" ); log.debug( "URI : {}" , request.getURI()); log.debug( "Method : {}" , request.getMethod()); log.debug( "Headers : {}" , request.getHeaders()); log.debug( "Request body: {}" , new String(body, "UTF-8" )); log.debug( "==========================request end================================================" ); } } private void logResponse(ClientHttpResponse response) throws IOException { if (log.isDebugEnabled()) { log.debug( "============================response begin==========================================" ); log.debug( "Status code : {}" , response.getStatusCode()); log.debug( "Status text : {}" , response.getStatusText()); log.debug( "Headers : {}" , response.getHeaders()); log.debug( "Response body: {}" , StreamUtils.copyToString(response.getBody(), Charset.defaultCharset())); log.debug( "=======================response end=================================================" ); } } } |
2) Register ClientHttpRequestInterceptor with RestTemplate
Now add above interceptor with RestTemplate’s
setInterceptors()
method.@Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); restTemplate.setRequestFactory( new BufferingClientHttpRequestFactory(clientHttpRequestFactory())); restTemplate.setMessageConverters(Collections.singletonList(mappingJacksonHttpMessageConverter())); restTemplate.setInterceptors( Collections.singletonList( new RequestResponseLoggingInterceptor()) ); return restTemplate; } |
Read More: RestTemplate with HttpClient
Demo
Start the application and execute any REST API using
RestTemplate
. Observe the logs.2018 - 06 -18T15: 27 : 38.203 + 0530 DEBUG ===========================request begin============================================= 2018 - 06 -18T15: 27 : 38.203 + 0530 DEBUG URI : <URL> 2018 - 06 -18T15: 27 : 38.203 + 0530 DEBUG Method : POST 2018 - 06 -18T15: 27 : 38.203 + 0530 DEBUG Headers : {Accept=[application/json], Content-Type=[application/json]} 2018 - 06 -18T15: 27 : 38.204 + 0530 DEBUG Request body: <BODY> 2018 - 06 -18T15: 27 : 38.204 + 0530 DEBUG ==========================request end================================================ 2018 - 06 -18T15: 27 : 38.203 + 0530 DEBUG ===========================response begin============================================= 2018 - 06 -18T15: 27 : 38.203 + 0530 DEBUG Status code : 200 2018 - 06 -18T15: 27 : 38.203 + 0530 DEBUG Status text : OK 2018 - 06 -18T15: 27 : 38.203 + 0530 DEBUG Headers : {Accept=[application/json], Content-Type=[application/json]} 2018 - 06 -18T15: 27 : 38.204 + 0530 DEBUG Response BODY : <BODY> 2018 - 06 -18T15: 27 : 38.204 + 0530 DEBUG ==========================response end================================================ |
Drop me your questions in comments section.
No comments:
Post a Comment