如果使用PUT,SpringMVC无法识别请求体参数

Sve*_*ges 17 rest jquery spring-mvc

也许这应该不起作用,但至少我想了解为什么.我在PUT身体中传递了一个简单的val = somevalue 但是spring发回了一个,400 Bad Request因为它似乎没有识别出val参数.

类似的请求适用于POST.可能是SpringMVC没有将PUT请求体识别为参数源吗?

Content=-Type 在两种情况下都正确设置为application/x-www-form-urlencoded.

spring拒绝调用的方法是:

@RequestMapping(value = "config/{key}", method = RequestMethod.PUT)
@ResponseBody
public void configUpdateCreate(final Model model, @PathVariable final String key, @RequestParam final String val,
        final HttpServletResponse response) throws IOException
{
    //...
}
Run Code Online (Sandbox Code Playgroud)

为了完整性,这里是jquery ajax调用.我看不出有什么不妥.客户端是Firefox 4或Chrome,两者都显示相同的结果.

$.ajax({
         url:url,
         type:'PUT',
         data:'val=' + encodeURIComponent(configValue),
         success: function(data) {...}
       });      
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

mrm*_*nly 19

我目前还不知道有什么工作,但这里的错误报告是"不会修复".我一直在打同样的问题

https://jira.springsource.org/browse/SPR-7414

更新:这是我的修复.我正在使用RequestBody注释.然后使用MultiValueMap.

http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/mvc.html#mvc-ann-requestbody

@RequestMapping(value = "/{tc}", method = RequestMethod.PUT) 
public void update(@PathVariable("tc") final String tc, 
@RequestBody MultiValueMap<String,String> body, HttpServletResponse response) {

    String name = body.getFirst("name");
// more code
}
Run Code Online (Sandbox Code Playgroud)


K. *_*ddy 12

Spring 3.1开始,使用org.springframework.web.filter.HttpPutFormContentFilter解决了这个问题.

<filter>
    <filter-name>httpPutFormContentFilter</filter-name>
    <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>httpPutFormContentFilter</filter-name>
    <servlet-name>rest</servlet-name>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)