Logging Request- and Response-Data From Requets Made Through RestTemplate

Logging request- and response-data for requests made through Spring’s RestTemplate is quite easy, if you know, what to do. But it is rather hard, if you have no clue where to start. Hence, I want to give you some hints in this post.

In its default configuration, the RestTemplate uses the HttpClient of the Apache HttpComponents package. You can verify this and the used version with the mvn-command


mvn dependency:tree

To enable for example logging of the HTTP-Headers send and received, you then simply can add the following to your logging configuration:


<logger name="org.apache.http.headers">
  <level value="debug"/>
</logger>

Possible Pitfalls

If that does not work, you should check, which version of the Apache HttpComponents your project actually is using, because the name of the logger has changed between version 3.x and 4.x. Another common cause of problems is, that the Apache HttpComponets uses Apache Commons Logging. If the jar for that library is missing, or if your project uses another logging library, the messages might get lost because of that.

Leave a Reply

Your email address will not be published. Required fields are marked *