In this tutorial, we’re going to learn how to implement efficient RestTemplate request/response logging. This is especially useful to debug exchange between two servers.
Unfortunately, Spring Boot doesn’t provide an easy way to inspect or log a simple JSON response body.
We’re going to explore several methods to log either HTTP headers or, which is the most interesting part, the HTTP body.
Note: the Spring RestTemplate will be deprecated, to be replaced by the WebClient. You can find a similar article using WebClient here: Logging Spring WebClient Calls.
Let’s start configuring the RestTemplate logger in the application.properties file:
As a result, we can see only basic information like the request URL, method, body, and response status:
However, the response body isn’t logged here, which is unfortunate because it’s the most interesting part.
To solve this, we’ll choose either Apache HttpClient or a Spring interceptor.
#rest #spring