getReader()/getInputStream() has already been called for this request
Posted 滴滴哒滴哒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
以上是关于getReader()/getInputStream() has already been called for this request的主要内容,如果未能解决你的问题,请参考以下文章
当我使用 servletRequest.getReader().lines().collect(Collectors.joining()) 时请求更改正文
request.getParameter()request.getInputStream()和request.getReader()
request.getParameter() request.getInputStream()和request.getReader()三者的区别
getReader()/getInputStream() has already been called for this request