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

前端 未结 4 448
北海茫月
北海茫月 2020-12-25 15:02

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 allo

4条回答
  •  春和景丽
    2020-12-25 15:54

    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.

提交回复
热议问题