jQuery 如果使用 PUT,SpringMVC 无法识别请求正文参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5894270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:58:22  来源:igfitidea点击:

SpringMVC is not recognizing request body parameters if using PUT

jqueryrestspring-mvc

提问by Sven Haiges

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 PUTbody but spring sends back a 400 Bad Requestas it does not seem to recognise the val parameter.

也许这应该行不通,但至少我想了解为什么。我在PUT正文中传递了一个简单的 val=somevalue但 spring 发回了 a,400 Bad Request因为它似乎无法识别 val 参数。

Similar request works with POST. Could it be SpringMVCis not recognizing the PUTrequest body as source for parameters?

类似的请求适用于POST. 可能是SpringMVC没有将PUT请求正文识别为参数源吗?

Content=-Typeis set correctly to application/x-www-form-urlencoded in both cases.

Content=-Type在这两种情况下都正确设置为 application/x-www-form-urlencoded。

The method that spring refuses to call is this:

spring拒绝调用的方法是这样的:

@RequestMapping(value = "config/{key}", method = RequestMethod.PUT)
@ResponseBody
public void configUpdateCreate(final Model model, @PathVariable final String key, @RequestParam final String val,
        final HttpServletResponse response) throws IOException
{
    //...
}

For completeness, here is the jquery ajax call. I cannot see anything wrong with that. Client is Firefox 4 or Chrome, both show the same result.

为了完整起见,这里是 jquery ajax 调用。我看不出有什么问题。客户端是 Firefox 4 或 Chrome,两者显示相同的结果。

$.ajax({
         url:url,
         type:'PUT',
         data:'val=' + encodeURIComponent(configValue),
         success: function(data) {...}
       });      

Any ideas?

有任何想法吗?

回答by mrmanly

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

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

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

更新:这是我的修复。我正在使用 RequestBody 注释。然后使用 MultiValueMap。

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

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
}

回答by K. Siva Prasad Reddy

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

Spring 3.1 开始,这是使用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>

回答by Slava Semushin

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

我没有适合您的解决方案,但在您的情况下,我尝试以下操作:

  • create page with form:form method="PUT"
  • declare HiddenHttpMethodFilterin web.xml
  • 创建页面 form:form method="PUT"
  • 声明HiddenHttpMethodFilterweb.xml

If this will works, then

如果这行得通,那么

  • change typefrom PUTto POSTin ajax call
  • add needed params which client has with form:formtag (something like _method)
  • 改变type来自PUTPOST在Ajax调用
  • 添加客户端具有form:form标记的所需参数(类似于_method

In other words, as I understand Spring emulates PUTbased on simple POSTwith special parameter. Just send to him what he wants.

换句话说,据我所知,SpringPUT基于POST带有特殊参数的simple进行模拟。把他想要的东西发给他就行了。

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

另请参阅:http: //stsmedia.net/spring-finance-part-2-spring-mvc-spring-30-rest-integration/和相关代码示例:http: //code.google.com/p/spring -财务经理/来源/浏览

HTH

HTH

回答by Ravi Bhatt

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

如上所述,这似乎是spring/servlet API. 实际上,PUT请求应该Request Body (or payload)对请求参数起作用而不是对请求参数起作用。从这个意义上说,servlet API & spring 的处理是正确的。

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

话虽如此,更好且更简单的解决方法是不从您的javascript/jQuery调用中传递任何数据元素,并将您的参数作为 url 本身的一部分传递。意思是,在 url 字段中设置参数,就像您在GET通话中所做的那样。

$.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.

现在这适用于简单的参数,我猜,不适用于复杂的 JSON 类型。希望这可以帮助。