为什么HttpServletRequest输入流为空?

我有这段代码,可以从请求输入流中读取输入,并使用JacksonMapper转换为POJO。它在带有guice支持的码头7集装箱中运行。

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

try {

RequestType requestType = mapper.readValue(req.getInputStream(), RequestType.class);

} Catch(Exception ex) {

....

}

}

但是,有时在负载下会引发以下异常。我已经检查了我的客户端,并且确定它发送了一个有效的json字符串。怎么了?负载下Jetty 7的预期行为吗?

java.io.EOFException: No content to map to Object due to end of input

at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2433)

at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2385)

at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1637)

at com.ea.wsop.user.LoginServlet.processRequest(LoginServlet.java:69)

at com.ea.wsop.user.LoginServlet.doPost(LoginServlet.java:63)

at com.ea.wsop.user.LoginServlet$$EnhancerByGuice$$a91c2ebd.CGLIB$doPost$0(<generated>)

at com.ea.wsop.user.LoginServlet$$EnhancerByGuice$$a91c2ebd$$FastClassByGuice$$c6f479ee.invoke(<generated>)

at com.google.inject.internal.cglib.proxy.$MethodProxy.invokeSuper(MethodProxy.java:228)

at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:72)

at com.ea.monitor.MethodExecutionTimer.invoke(MethodExecutionTimer.java:130)

at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:72)

at com.google.inject.internal.InterceptorStackCallback.intercept(InterceptorStackCallback.java:52)

at com.ea.wsop.user.LoginServlet$$EnhancerByGuice$$a91c2ebd.doPost(<generated>)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

at com.ea.wsop.user.LoginServlet$$EnhancerByGuice$$a91c2ebd.CGLIB$service$8(<generated>)

at com.ea.wsop.user.LoginServlet$$EnhancerByGuice$$a91c2ebd$$FastClassByGuice$$c6f479ee.invoke(<generated>)

at com.google.inject.internal.cglib.proxy.$MethodProxy.invokeSuper(MethodProxy.java:228)

at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:72)

at com.ea.monitor.MethodExecutionTimer.invoke(MethodExecutionTimer.java:130)

at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:72)

at com.google.inject.internal.InterceptorStackCallback.intercept(InterceptorStackCallback.java:52)

at com.ea.wsop.user.LoginServlet$$EnhancerByGuice$$a91c2ebd.service(<generated>)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

at com.ea.wsop.user.LoginServlet$$EnhancerByGuice$$a91c2ebd.CGLIB$service$9(<generated>)

at com.ea.wsop.user.LoginServlet$$EnhancerByGuice$$a91c2ebd$$FastClassByGuice$$c6f479ee.invoke(<generated>)

at com.google.inject.internal.cglib.proxy.$MethodProxy.invokeSuper(MethodProxy.java:228)

at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:72)

at com.ea.monitor.MethodExecutionTimer.invoke(MethodExecutionTimer.java:130)

at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:72)

at com.google.inject.internal.InterceptorStackCallback.intercept(InterceptorStackCallback.java:52)

at com.ea.wsop.user.LoginServlet$$EnhancerByGuice$$a91c2ebd.service(<generated>)

at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263)

回答:

如果它已经被预先消耗,它将为空。这将隐式进行,每当你打电话getParameter()getParameterValues()getParameterMap()getReader(),等上HttpServletRequest。确保不要调用任何一种方法,这些方法本身在调用前需要从请求主体收集信息getInputStream()。如果您的servlet没有这样做,那么开始检查映射到同一URL模式的servlet过滤器。


这似乎是GAE 1.5特有的。也可以看看

  • http://code.google.com/p/googleappengine/issues/detail?id=5161
  • http://code.google.com/p/googleappengine/issues/detail?id=5898

除非他们解决问题,否则恐怕没有解决方案/变通办法。您可以 尝试 检查a中是否可用Filter,如果可以,则将其复制并存储为request属性。但这

可能会 影响某些GAE Servlet的进一步处理。

以上是 为什么HttpServletRequest输入流为空? 的全部内容, 来源链接: utcz.com/qa/405180.html

回到顶部