我有一个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");
}
}
我的应用程序上下文定义如下:
<?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>
我正在使用curl测试这个,如下所示:
curl -d asd = 123 -d qwe = 456 http:// localhost:8080 / requestbody
任何想法或帮助都受到欢迎!
4 回答
这是
ServletServerHttpRequest
的代码片段,扩展了HttpInputMessage
. 我非常肯定这是您在代码中使用的实现:换句话说,请求主体应被读取为
HttpServletRequest
对象的输入流 .请求's input stream is not valid in several situations, but I can' t目前找到了正确的文档 . 例如,如果在发布请求上调用
request.getParameter()
,则tomcat必须读取输入流才能解释参数,因此在读取输入流之后,它会为空,因为它已经到达终点 .也许你在拦截器中某处或者在web.xml中定义的过滤器中调用
getParameter
. 另一个选择是Spring正在为你做这件事,例如,如果你的控制器有一些复杂的@RequestMapping
的其他方法(比如读取参数值或 Headers 值) .我有两个建议:
添加一个servlet过滤器(在spring有机会行动之前),并用你自己的包装器包装请求(只需扩展HttpServletRequestWrapper) . 这样,您可以在请求对象的某些方法中放置断点或日志消息,并查看谁在调用它们 .
使用pojo对象参数,并设置绑定 . 这似乎是一种更清晰的阅读帖子数据的方式 .
你如何将消息发布到这个URL?您是否肯定HTTP请求包含您认为它的功能?我建议从图片中删除任何Web浏览器并下拉到像
curl
这样的低级别,这样您就可以自己发送任何类型的HTTP消息 .有一个类似的问题 - spring 控制器收到的字符串总是空的 . 用我的 spring 配置进行修补但没有结果 . 最后问题是客户实际上并没有发送任何正文!(由于我的一些错字)
如果发现有类似的错误,如果客户端的有效负载实际上是非空的,则值得检查一次 .
您的XML可能无法编组到JAXB对象中的另一个原因与XML中的命名空间有关 .
1.8.101之后的Java版本对解析命名空间XML更严格 . 见JAXB doesn't unmarshall after updating java from 1.8.0_77 to 1.8.0_121
在我的情况下,我看到一个请求正文,其中包含所有空值,并且抛出 no 异常以指示XML解析失败 .