1. Overview

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.

2. Basic Logging With RestTemplate

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

Spring RestTemplate Request/Response Logging
19.60 GEEK