has already been called for this request
程序员文章站
2024-02-03 11:21:22
...
java.lang.IllegalStateException: getReader() has already been called for this request
at org.apache.catalina.connector.Request.getInputStream(Request.java:1056)
at org.apache.catalina.connector.RequestFacade.getInputStream(RequestFacade.java:365)
at javax.servlet.ServletRequestWrapper.getInputStream(ServletRequestWrapper.java:149)
at org.springframework.http.server.ServletServerHttpRequest.getBody(ServletServerHttpRequest.java:212)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver$EmptyBodyCheckingHttpInputMessage.<init>(AbstractMessageConverterMethodArgumentResolver.java:317)
HttpServletRequest的 getReader()和getInputStream()只能调用一次,Request body是流方式请求,读取一次之后,就不能再用了,看下源码:
@Override
public BufferedReader getReader() throws IOException {
if (usingInputStream) {
throw new IllegalStateException(sm.getString("coyoteRequest.getReader.ise"));
}
// InputBuffer has no easily accessible reference chain to the Context
// to check for a default request character encoding at the Context.
// Therefore, if a Context default should be used, it is set explicitly
// here. Need to do this before setting usingReader.
if (coyoteRequest.getCharacterEncoding() == null) {
// Nothing currently set explicitly.
// Check the content
Context context = getContext();
if (context != null) {
String enc = context.getRequestCharacterEncoding();
if (enc != null) {
// Explicitly set the context default so it is visible to
// InputBuffer when creating the Reader.
setCharacterEncoding(enc);
}
}
}
usingReader = true;
inputBuffer.checkConverter();
if (reader == null) {
reader = new CoyoteReader(inputBuffer);
}
return reader;
}
@Override
public ServletInputStream getInputStream() throws IOException {
if (usingReader) {
throw new IllegalStateException(sm.getString("coyoteRequest.getInputStream.ise"));
}
usingInputStream = true;
if (inputStream == null) {
inputStream = new CoyoteInputStream(inputBuffer);
}
return inputStream;
}
可以看到Request.java调用getReader()方法后,usingReader被设置为true,getInputStream()方法会读取usingReader变量,如果为true就会报错
上一篇: 交换两数组中的元素使得这两个数组的差最小
推荐阅读
-
has already been called for this request
-
解决流只能读一次的问题,getInputStream() has already been called for this request
-
tomcat6下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
-
Zuul Filter过滤器返回信息提示getWriter() has already been called for this response
-
解决java.lang.IllegalStateException: getWriter() has already been called for this response
-
java.lang.IllegalStateException: getOutputStream() has already been called for this response
-
getOutputStream() has already been called for this response解决方案
-
getOutputStream() has already been called for this response解决方案
-
.NET CORE 使用Session报错:Session has not been configured for this application or request
-
getOutputStream() has already been called for this response