How to read a JSON request inside a Spring Boot filter?

Let’s say you are using a filter in your spring boot application.

Spring boot (Spring Security) already internally uses many filters to filter requests coming to your application.

If you are going to create a custom filter you can do so by implementing Filter interface from javax servlet package or by extending GenericFilterBean/ OncePerRequestFilter provided by Spring.

Let’s take the former route.

Here is a filter created in a sample boot application:

@Component
public class TestFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		//YOUR LOGIC HERE

		chain.doFilter(request, response);
		return;

	}

}

You do three things here:

  • Implement Filter interface
  • Override doFilter method
  • Pass the request to the next filter using chain.doFilter() method call.

The above filter does nothing, it just forwards the request to the next filter in the filter chain.

But what if you want to read the request and do some processing on it.

You may try something like this:

@Component
public class TestFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		HttpServletRequest httpReq = (HttpServletRequest) request;

		byte[] body = StreamUtils.copyToByteArray(request.getInputStream());

		Map<String, Object> jsonRequest = new ObjectMapper().readValue(body, Map.class);

		System.out.println(jsonRequest);

		chain.doFilter(request, response);
		return;

	}

}

The above code won’t work!

This is because you can read a http servlet request only once and if you read it in your filter , the other filters in Spring Security Filter Chain can’t read them and so you get an exception:

So how to resolve this?

You create a wrapper around the http servlet request object and do your processing on it.

You need to create two wrappers actually.

One for the HttpServletRequest object

Another for the ServletInputStream object.

Here are the two wrapper implementations:

HttpServletRequestWrapper:

package com.example.filter;

import java.io.IOException;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.springframework.util.StreamUtils;

public class RequestWrapper extends HttpServletRequestWrapper {

	private byte[] body;

	public RequestWrapper(HttpServletRequest request) throws IOException {
		super(request);

		this.body = StreamUtils.copyToByteArray(request.getInputStream());
	}

	@Override
	public ServletInputStream getInputStream() throws IOException {
		return new ServletInputStreamWrapper(this.body);

	}

}

You create the wrapper by extending HttpServletRequestWrapper .

You implement a constructor inside the wrapper where you get the actual httpservlet request object and store its contents in a byte array.

You also override the method getInputStream() and instead of returning the input stream from the original http servlet request object you return it from another wrapper you are going to create (ServletInputStreamWrapper).

Here is that wrapper:

package com.example.filter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;

public class ServletInputStreamWrapper extends ServletInputStream{

	
	private InputStream inputStream;
	
	
	public  ServletInputStreamWrapper(byte[] body) {
		
		
		this.inputStream = new ByteArrayInputStream(body);
	}
	
	@Override
	public boolean isFinished() {
		
		try {
			
			return inputStream.available() == 0;
			
		}catch(Exception e) {
			
			return false;
		}
	}

	@Override
	public boolean isReady() {
		return true;
	}

	@Override
	public void setReadListener(ReadListener listener) {

		
	}

	@Override
	public int read() throws IOException {
		return this.inputStream.read();
	}

}

You need to extend ServletInputStream class to create this wrapper.

Create a constructor inside this wrapper where you get the byte array populated in the first wrapper ( HttpServletRequestWrapper) and convert it into an input stream . It is this stream which you are going to use to read the input request.

And then you override the following methods:

  • isReady(0
  • isFinished()
  • read()
  • setReadListener() //this can be left empty

isReady() can always return true

isFinished() just checks if there is any data in the inputStream

read() reads from the input stream

Once the wrappers are created , you can read your json request inside your Filter using the below code:

package com.example.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class TestFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		System.out.println("Inside Filter");
		

//Wrap the request 
		RequestWrapper wrapper = new RequestWrapper((HttpServletRequest) request);

//Get the input stream from the wrapper and convert it into byte array
		byte[] body = StreamUtils.copyToByteArray(wrapper.getInputStream());

// use jackson ObjectMapper to convert the byte array to Map (represents JSON)
		Map<String, Object> jsonRequest = new ObjectMapper().readValue(body, Map.class);

		System.out.println(jsonRequest);

		chain.doFilter(wrapper, response);
		return;

	}

}

Now let’s test this

I created a simple REST API :

package com.example.filter;

import java.util.Map;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@PostMapping("/test")
	public Map<String, Object> test(@RequestBody Map<String, Object> request) {

		request.put("success", "true");

		return request;
	}
}

Let’s run the application and see if the input passed to the above API can be read through the filter:

The json request got read inside the filter!


Posted

in

,

by

Comments

8 responses to “How to read a JSON request inside a Spring Boot filter?”

  1.  Avatar
    Anonymous

    Te quiero <3

  2.  Avatar
    Anonymous

    you are my hero. ContentCachingRequestWrapper only seems to work if you read it AFTER the doFilter gets processed otherwise all my springboot controllers break. This actually works.

    1. Vijay SRJ Avatar
      Vijay SRJ

      Thank you 🙂

  3. Peter Hollas Avatar
    Peter Hollas

    Just stumbled across this, thanks for posting a full working example. It would be nice if Spring provided some way to do this without jumping through hoops, it can’t be an uncommon problem.

    1. Vijay SRJ Avatar
      Vijay SRJ

      thank you very much for the feedback , yeah Spring should provide an simpler solution for this

  4.  Avatar
    Anonymous

    i am getting org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing:

  5. Jodeilson Avatar
    Jodeilson

    You saved 2 days of headache, many thanks 😀

    1. Vijay SRJ Avatar
      Vijay SRJ

      Thanks for leaving the comment, you are welcome 😊

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading