How to log requests and their payloads in Spring

From time to time we may need to log our requests in order to get some information. Personally, I’m writing this short article, because we need to find out the reason why jackson throws 400 error status.

Luckily, it’s very easy to log your requests. Spring has a class AbstractRequestLoggingFilter, which has three concrete classes, which you can potentially use:

ServletContextRequestLoggingFilter

Log4jNestedDiagnosticContextFilter

CommonsRequestLoggingFilter

The last one is the guy we need. It’s pretty straightforward how to configure this class: just declare it in your context: