最近的工做中遇到一個問題,在使用Spring時,RequestMethod.POST方法可以接收到參數,但RequestMethod.PUT卻接收不到傳進來的參數,代碼大體以下:java
@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的參數是能收到,其餘的不行。在網上搜索資料找到解決方法:web
stackoverflow的原文地址:http://stackoverflow.com/questions/5894270/springmvc-is-not-recognizing-request-body-parameters-if-using-put spring
說是Servlet API的一些問題:https://jira.spring.io/browse/SPR-7414 mvc
有意思的是這個bug在spring的 Resolution是Won't Fix。
app
我目前的解決方法是在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>
能夠接收到數據了,另外一種方法沒測試,不知是否可行。ui