Can Spring MVC have request parameters for an HTTP PUT method, or must I use post? Which should I use to be RESTful?

泪湿孤枕 提交于 2019-11-30 03:15:12
Arjan

Since Spring 3.1, HttpPutFormContentFilter can be used to handle application/x-www-form-urlencoded data:

Filter that makes form encoded data available through the ServletRequest.getParameter*() family of methods during HTTP PUT requests.

The Servlet spec requires form data to be available for HTTP POST but not for HTTP PUT requests. This filter intercepts HTTP PUT requests where content type is 'application/x-www-form-urlencoded', reads form encoded content from the body of the request, and wraps the ServletRequest in order to make the form data available as request parameters just like it is for HTTP POST requests.

For other incoming data, such as JSON, you'll need @RequestBody as explained in JQuery, Spring MVC @RequestBody and JSON - making it work together, to not run into a 415 Unsupported Media Type.

Spring controllers support GET/HEAD/POST/PUT/DELETE/OPTIONS/TRACE, but since your browser may not be able to send these request methods, it wont work for you.

The workaround is to use the "org.springframework.web.filter.HiddenHttpMethodFilter" provided by Spring. It requires you to pass a hidden parameter for the request method. The default parameter supported by this filter is "_method".

Check the javadoc of the filter for more info.

This, as suggest above, seems to be a bug in spring/servlet API. In reality PUT requests are supposed to work on Request Body (or payload) and not on Request Parameters. In that sense, servlet API & spring's handling is correct.

Having said that, a better and much easier workaround is to pass no data element from your javascript/jQuery call and pass your parameters as part of the url itself. meaning, set parameters in the url field the way you would do in a GET call.

$.ajax({
            url: "/help/" + helpDocumentId + "/vote.json" + "?param1=param2Val&..",
            type: "PUT",
            data: "",
            success: function(response) {
                // ....
            }
     });

now this works for simple parameters, i guess, will not work for complex JSON types. Hope this helps.

I followed the recommendation in the comments and changed @RequestParam to @RequestBody and it just worked (my parameter is a String).

I agree this is a bug in Spring because the exact same code that fails in my production environment (when using @RequestParam) works fine in localhost.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!