getReader()/getInputStream() has already been called for this request

 项目中需要在filter对request中body中的数据进行处理,发现了这个问题
getRead() has already been called for this request/getInputStream() has already been called for this request

工程读取request body的方法

public class RequestReadUtils {
    
    private static final int BUFFER_SIZE = 1024 * 8;

    public static String read(HttpServletRequest request) throws IOException {
        BufferedReader bufferedReader = request.getReader();
        StringWriter writer = new StringWriter();
        write(bufferedReader,writer);
        return writer.getBuffer().toString();
    }

    public static long write(Reader reader,Writer writer) throws IOException {
        return write(reader, writer, BUFFER_SIZE);
    }

    public static long write(Reader reader, Writer writer, int bufferSize) throws IOException
    {
        int read;
        long total = 0;
        char[] buf = new char[bufferSize];
        while( ( read = reader.read(buf) ) != -1 ) {
            writer.write(buf, 0, read);
            total += read;
        }
        return total;
    }
}

根本原因是
request中的getRead() 和 getInputStream()在读取一次后标记为-1,无法再次被读取
而可以看到@RequestBody在ServletServerHttpRequest中,也调用了getInputStream()方法 

public InputStream getBody() throws IOException {
        return (InputStream)(isFormPost(this.servletRequest) ? getBodyFromServletRequestParameters(this.servletRequest) : this.servletRequest.getInputStream());
    }
private static boolean isFormPost(HttpServletRequest request) {
        String contentType = request.getContentType();
        return contentType != null && contentType.contains("application/x-www-form-urlencoded") && HttpMethod.POST.matches(request.getMethod());
    }

所以如果数据在filter中被读取,将无法在@RequestBody中再次读取。

此时解决方案,将request进行包装(装饰者模式),并重写getInputStream()和getRead()方法

 

public class MyRequestWrapper extends HttpServletRequestWrapper {

    private final String body;


    public MyRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        this.body = RequestReadUtils.read(request);
    }

    public String getBody() {
        return body;
    }



    @Override
    public ServletInputStream getInputStream()  {
        final ByteArrayInputStream bais = new ByteArrayInputStream(body.getBytes());
        return new ServletInputStream() {

            @Override
            public boolean isFinished() {
                return false;
            }

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

            @Override
            public void setReadListener(ReadListener readListener) {

            }

            @Override
            public int read(){
                return bais.read();
            }
        };
    }

    @Override
    public BufferedReader getReader(){
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }


}

我们设置一个私有变量body用于存储原始request的body数据,并重写getReader()和getInputStream()方法,这样我们在filter处理了body数据,又能在controller中用@RequestBody中获取到数据了,最后附上filter代码

public class MyFilter implements Filter {
    


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
      //TODO something
    }

    @Override
    public void destroy() {
      //TODO something
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //取Body数据
        MyRequestWrapper requestWrapper = new MyRequestWrapper(request);
        String body = requestWrapper.getBody();
        //TODO something
        filterChain.doFilter(requestWrapper != null ? requestWrapper :request,servletResponse);
       
    }
}

 

你可能感兴趣的:(项目笔记,filter,@RequestBody,has,already,b)