How to modify request body before reaching controller in spring boot

How to modify request body before reaching controller in Spring Boot

Spring Boot provides various ways to modify the request body before it reaches the controller. Here are a few examples:

  1. Using a custom filter: Intercepting the request using a custom filter allows you to modify the request body before it reaches the controller. You can create a filter by implementing the javax.servlet.Filter interface. In the doFilter method, you can access and modify the request body using HttpServletRequestWrapper or InputStream. Here’s an example:
  2. 
    import org.springframework.web.filter.OncePerRequestFilter;
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import java.io.BufferedReader;
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class CustomFilter extends OncePerRequestFilter {
    
      @Override
      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request) {
          @Override
          public ServletInputStream getInputStream() throws IOException {
            // Modify request body here
            String requestBody = // ...
    
            final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(requestBody.getBytes());
    
            return new ServletInputStream() {
              public int read() throws IOException {
                return byteArrayInputStream.read();
              }
            };
          }
    
          @Override
          public BufferedReader getReader() throws IOException {
            return new BufferedReader(new InputStreamReader(this.getInputStream()));
          }
        };
    
        filterChain.doFilter(requestWrapper, response);
      }
    }
        
  3. Using a custom interceptor: Spring Boot provides a way to intercept requests using a custom interceptor. You can create an interceptor by implementing the org.springframework.web.servlet.HandlerInterceptor interface. In the preHandle method, you can access and modify the request body using HttpServletRequestWrapper or InputStream. Here’s an example:
  4. 
    import org.springframework.web.servlet.HandlerInterceptor;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.BufferedReader;
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class CustomInterceptor implements HandlerInterceptor {
    
      @Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request) {
          @Override
          public ServletInputStream getInputStream() throws IOException {
            // Modify request body here
            String requestBody = // ...
    
            final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(requestBody.getBytes());
    
            return new ServletInputStream() {
              public int read() throws IOException {
                return byteArrayInputStream.read();
              }
            };
          }
    
          @Override
          public BufferedReader getReader() throws IOException {
            return new BufferedReader(new InputStreamReader(this.getInputStream()));
          }
        };
    
        return true;
      }
    }
        

You can then register these custom filters or interceptors in your Spring Boot application’s configuration class. These modifications to the request body will be applied before it reaches the controller.

Leave a comment