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: im