RestTemplate Custom Interceptors
Spring Framework provides a very handy Http connection class specialized on REST communication named RestTemplate. The RestTemplate has a wealth of REST call methods that can be easily used to quickly invoke external / remote REST API endpoints with no mess of opening and closing sockets, or even marshal and unmarshaling java objects.
However such ease of use comes with a caveat, whenever we need greater control with the connection, or the marshal and unmarshal process of java objects we can't do so easily as when we are using HttpConnection class.
Nevertheless, one way we can gain some control to the Request and Response is through ClientHttpRequestInterceptor interface. When registered to the RestTemplate object, this interface grants us access to the Request object, request body data and also Response object.
The example below shows how we can utilize ClientHttpRequestInterceptor interface to log Request and Response during a typical communication with external API:
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;
public class LoggingInterceptor implements ClientHttpRequestInterceptor {
private final static Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class);
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes,
ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
ClientHttpResponse httpResponse = clientHttpRequestExecution.execute(httpRequest, bytes);
if(logger.isDebugEnabled()) {
logger.debug("Request Body: " + new String(bytes));
logger.debug("Response Code: " + httpResponse.getStatusCode());
}
return response;
}
}
And to register the Interceptor:
@Autowired
RestTemplate template;
if(template.getInterceptors() == null) {
template.setInterceptors(new ArrayList<>());
}
template.getInterceptors().add(new LoggingInterceptor());
Happy intercepting!
However such ease of use comes with a caveat, whenever we need greater control with the connection, or the marshal and unmarshal process of java objects we can't do so easily as when we are using HttpConnection class.
Nevertheless, one way we can gain some control to the Request and Response is through ClientHttpRequestInterceptor interface. When registered to the RestTemplate object, this interface grants us access to the Request object, request body data and also Response object.
The example below shows how we can utilize ClientHttpRequestInterceptor interface to log Request and Response during a typical communication with external API:
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;
public class LoggingInterceptor implements ClientHttpRequestInterceptor {
private final static Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class);
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes,
ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
ClientHttpResponse httpResponse = clientHttpRequestExecution.execute(httpRequest, bytes);
if(logger.isDebugEnabled()) {
logger.debug("Request Body: " + new String(bytes));
logger.debug("Response Code: " + httpResponse.getStatusCode());
}
return response;
}
}
And to register the Interceptor:
@Autowired
RestTemplate template;
if(template.getInterceptors() == null) {
template.setInterceptors(new ArrayList<>());
}
template.getInterceptors().add(new LoggingInterceptor());
Happy intercepting!
Comments
Post a Comment