Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have a controller action I think should be an HTTP PUT, but Spring is complaining when I try and use @RequestParam in the controller action. Is request parameters not allowed for HTTP PUT methods, and is that why Spring is rejecting it?

@RequestMapping(value = "/{helpDocumentId}/vote", method = RequestMethod.PUT)
public void voteHelpfulness(@PathVariable long helpDocumentId, @RequestParam boolean isHelpful) {
    helpManager.voteOnHelpDocument(helpDocumentId, isHelpful);
}

When executed, it throws this error:

org.springframework.web.bind.MissingServletRequestParameterException: Required boolean parameter 'isHelpful' is not present

Of course, the isHelpful parameter IS present. I can make the above code work perfectly for HTTP POST, so I know this isn't the problem.

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

Is PUT the correct http method? This action modifies the helpDocument, but it doesn't create one.

like image 438
egervari Avatar asked Nov 23 '11 23:11

egervari


2 Answers

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.

like image 183
Arjan Avatar answered Sep 22 '22 13:09

Arjan


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.

like image 22
praveenj Avatar answered Sep 19 '22 13:09

praveenj