Joh*_*nco 9 java spring spring-mvc
我有一个Spring 3.0.5.RELEASE的应用程序试图使用@RequestBody获取帖子的完整内容.调用该方法,但传递的字符串始终为空.我通过放置断点检查了StringHttpMessageConverter是否被调用,但内部HttpInputMessage是空的.
我已经看过Jetty和Tomcat的这个问题,所以我放弃了容器的问题.
这是我的样本控制器:
@Controller
@RequestMapping("/")
public class SubscriptionController {
@RequestMapping(value = "/requestbody", method = RequestMethod.POST)
public ModelAndView mycustomAction(@RequestBody String body) {
// body is always empty
Logger.getLogger(this.getClass()).debug("REQUEST BODY '" + body + "'");
return new ModelAndView("empty");
}
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序上下文定义如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Enable auto detection of controllers -->
<context:component-scan base-package="com.big.viajerotelcel.controller" />
<!--
use annotation driven mvc and one single validator with JSR-303
standard
-->
<mvc:annotation-driven />
<!--
Message source for this context, loaded from localized "messages_xx"
files
-->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames" value="classpath:i18n/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<!-- Declare the Interceptor -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="locale" />
</mvc:interceptors>
<!-- Declare the Resolver -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<!-- will load Tiles definitions! -->
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/general.xml</value>
</list>
</property>
</bean>
<!-- Tiles view resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<!-- Configure the multipart resolver -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--
one of the properties available; the maximum file size in bytes (5MB)
-->
<property name="maxUploadSize" value="5120000" />
</bean>
<!-- Adding these lines has no effect, the StringHttpMessageConverter is called either way -->
<!-- <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/>-->
<!-- -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">-->
<!-- <property name="messageConverters">-->
<!-- <list>-->
<!-- <ref bean="stringHttpMessageConverter"/>-->
<!-- </list>-->
<!-- </property>-->
<!-- </bean>-->
</beans>
Run Code Online (Sandbox Code Playgroud)
我正在使用curl测试这个,如下所示:
curl -d asd = 123 -d qwe = 456 http:// localhost:8080/requestbody
任何想法或帮助都受到欢迎!
这是一个ServletServerHttpRequest扩展的代码片段HttpInputMessage.我非常肯定这是您在代码中使用的实现:
public InputStream getBody() throws IOException {
return this.servletRequest.getInputStream();
}
Run Code Online (Sandbox Code Playgroud)
换句话说,请求主体意味着被读取为HttpServletRequest对象的输入流.
请求的输入流在几种情况下无效,但我目前找不到正确的文档.例如,如果你调用request.getParameter()一个post请求,tomcat必须读取输入流才能解释参数,因此当你读取输入流时,它是空的,因为它已经到了最后.
也许你是getParameter在拦截器中调用某个地方,或者是在web.xml中定义的过滤器.另一个选择是Spring正在为你做这件事,例如,如果你的控制器有一些其他复杂@RequestMapping的方法(比如读取参数值或标题值).
我有两个建议:
添加一个servlet过滤器(在spring有机会行动之前),并用你自己的包装器包装请求(只需扩展HttpServletRequestWrapper).这样,您可以在请求对象的某些方法中放置断点或日志消息,并查看谁在调用它们.
使用pojo对象参数,并设置绑定.这似乎是一种更清晰的阅读帖子数据的方式.
| 归档时间: |
|
| 查看次数: |
12209 次 |
| 最近记录: |