Spring的@RequestBody在POST上提供空字符串


问题内容

我有一个使用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

任何想法或帮助都将受到欢迎!


问题答案:

这是的ServletServerHttpRequest扩展代码段HttpInputMessage。我非常肯定这是您在代码中使用的实现:

public InputStream getBody() throws IOException {
    return this.servletRequest.getInputStream();
}

换句话说,请求主体应作为HttpServletRequest对象的输入流读取。

该请求的输入流在某些情况下无效,但目前无法找到正确的文档。例如,如果您调用request.getParameter()发布请求,则tomcat必须读取输入流才能解释参数,因此,当您读取输入流后,由于它已经到达末尾,因此它为空。

也许您正在调用getParameter拦截器中的某个位置,或者正在web.xml中定义的过滤器中调用。另一个选择是,例如,如果您的控制器有一些其他带有复数@RequestMappings的方法(例如读取参数值或标头值),则Spring会为您执行此操作。

我有两个建议给您:

  1. 添加一个servlet过滤器(在spring有机会采取行动之前),然后使用您自己的包装器包装请求(只需扩展HttpServletRequestWrapper即可)。这样,您可以在请求对象的某些方法上放置断点或记录消息,并查看谁在调用它们。

  2. 使用pojo对象参数,并设置绑定。似乎是一种更清洁的方式来读取帖子数据。