最近的工作中遇到一个问题,在使用Spring时,RequestMethod.POST方法能够接收到参数,但RequestMethod.PUT却接收不到传进来的参数,代码大致如下:
@Controller @RequestMapping("/v1") public class AccountController { @RequestMapping(value = "/account/{id}", method = RequestMethod.PUT) @ResponseBody public Object updateUserInfo(@PathVariable Long id, @RequestParam(value = "phone", required = false) String phone, @RequestParam(value = "nickName", required = false) String userName) { // do something.... } }
id的参数是能收到,其他的不行。在网上搜索资料找到解决方法:
stackoverflow的原文地址:http://stackoverflow.com/questions/5894270/springmvc-is-not-recognizing-request-body-parameters-if-using-put
说是Servlet API的一些问题:https://jira.spring.io/browse/SPR-7414
有意思的是这个bug在spring的 Resolution是Won‘t Fix。
我目前的解决方法是在web.xml中加入:
<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>
可以接收到数据了,另一种方法没测试,不知是否可行。
Spring的RequestMethod.PUT方法接收不到参数
原文:http://my.oschina.net/u/1586157/blog/392470