SpringMVC is not recognizing request body parameters if using PUT

后端 未结 4 1112
我在风中等你
我在风中等你 2020-12-04 16:50

Maybe this is supposed to not work, but at least I\'d like to understand why then. I am passing a simple val=somevalue in the PUT body but spring sends back a <

相关标签:
4条回答
  • 2020-12-04 17:27

    I don't have right solution for you, but in your case I try following:

    • create page with form:form method="PUT"
    • declare HiddenHttpMethodFilter in web.xml

    If this will works, then

    • change type from PUT to POST in ajax call
    • add needed params which client has with form:form tag (something like _method)

    In other words, as I understand Spring emulates PUT based on simple POST with special parameter. Just send to him what he wants.

    See also: http://stsmedia.net/spring-finance-part-2-spring-mvc-spring-30-rest-integration/ and related code examples there: http://code.google.com/p/spring-finance-manager/source/browse

    HTH

    0 讨论(0)
  • 2020-12-04 17:28

    Since Spring 3.1, this is resolved using org.springframework.web.filter.HttpPutFormContentFilter.

    <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>
    
    0 讨论(0)
  • 2020-12-04 17:32

    I don't know of a work around at this point, but here is the bug report that is a "Won't Fix." I've been fighting the same issue

    https://jira.springsource.org/browse/SPR-7414

    Update: Here is my fix. I'm using RequestBody annotation. Then using MultiValueMap.

    http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/mvc.html#mvc-ann-requestbody

    @RequestMapping(value = "/{tc}", method = RequestMethod.PUT) 
    public void update(@PathVariable("tc") final String tc, 
    @RequestBody MultiValueMap<String,String> body, HttpServletResponse response) {
    
        String name = body.getFirst("name");
    // more code
    }
    
    0 讨论(0)
  • 2020-12-04 17:34

    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: "yoururl" + "?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.

    0 讨论(0)
提交回复
热议问题