How to trace HTTP Requests in Spring Boot using CommonsRequestLoggingFilter

This tutorial shows how to trace incoming HTTP requests using CommonsRequestLoggingFilter.

There are several options to trace the incoming HTTP Requests of a Spring Boot 2.x application. One of the simplest option is to use the CommonsRequestLoggingFilter which can be added as a Bean in any configuration class. See the following example:

package com.example.testrest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;

public class RequestLoggerFilter {
  public CommonsRequestLoggingFilter logFilter() {
    CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
    filter.setAfterMessagePrefix("REQUEST DATA : ");
    return filter;

You also need to set the logging level for CommonsRequestLoggingFilter in the file: 

Or as an alternative, in logback.xml:

<?xml version="1.0" encoding="UTF-8"?><logger name="org.springframework.web.filter.CommonsRequestLoggingFilter">
   <level value="DEBUG"/>

When added, you will be able to see the incoming request in your Spring Boot Console:

2020-04-09 15:53:06.554 DEBUG 13536 --- [nio-8080-exec-1] o.s.w.f.CommonsRequestLoggingFilter      : Before request [uri=/list] 2020-04-09 15:53:06.630 DEBUG 13536 --- [nio-8080-exec-1] o.s.w.f.CommonsRequestLoggingFilter      : REQUEST DATA : uri=/list] 

On the other hand, if you want to receive the HTTP Request as a Stream, you can do that by extending the HandlerInterceptorAdapter class:

@Component public class CustomHandlerInterceptorAdapter extends HandlerInterceptorAdapter {
  @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    ServletRequest servletRequest = new ContentCachingRequestWrapper(request);
    // Read inputStream and log it
    return true;

Mind it, as you are dealing with InputStreams, you cannot read them twice!