简体   繁体   English

弹簧和ExtJS“400错误请求”与PUT但不与POST

[英]Spring and ExtJS “400 Bad Request” with PUT but not with POST

I'm trying to send parameters with PUT from JavaScript to a Spring application. 我正在尝试将带有PUT的参数从JavaScript发送到Spring应用程序。 Here is the @RequestMapping in a Spring Controller: 这是Spring Controller中的@RequestMapping:

@RequestMapping(value = "toggle-paid-action", method = RequestMethod.PUT)
@ResponseBody
public final String togglePaid(@RequestParam final int year, 
    @RequestParam final String docType, @RequestParam final int number) {

And here the JavaScript snippet that is supposed to send those parameters. 这里是应该发送这些参数的JavaScript代码段。

Ext.Ajax.request({
    params: {year: year, docType: docType, number: number},
    url: 'toggle-paid-action',
    method: 'PUT',

However, I get a "400 Bad Request" every time with description "The request sent by the client was syntactically incorrect ()". 但是,我每次都会收到“400 Bad Request”描述“客户端发送的请求在语法上不正确()”。

If I check with Firebug, there is a PUT tab with all my parameters, and the parameters are correctly spelled since if I switch from PUT to POST on both sides everything works. 如果我查看Firebug,有一个带有我所有参数的PUT选项卡,并且参数拼写正确,因为如果我从PUT切换到POST两端都可以正常工作。

I was wondering what could be the problem, is PUT limited to @PathVariable parameters, or it can send also POST-like parameters? 我想知道可能是什么问题,PUT是否仅限于@PathVariable参数,还是它也可以发送类似POST的参数?

I suppose you can't pass parameters to spring using request method PUT as there is a restriction in the servlet API. 我想你不能使用请求方法PUT将参数传递给spring,因为servlet API中存在限制。 You can only work with PUT methods implementing a restful service, passing data as the request body , in other cases (like Spring MVC Databinding) PUT won't work. 您只能使用PUT方法实现一个restful服务,将数据作为请求体传递,在其他情况下(如Spring MVC Databinding)PUT将不起作用。 see SpringMVC is not recognizing request body parameters if using PUT 如果使用PUT,请参阅SpringMVC无法识别请求体参数

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

This, as suggest above, seems to be a bug in spring/servlet API . 如上所述,这似乎是spring/servlet API一个错误。 In reality PUT requests are supposed to work on Request Body (or payload) and not on Request Parameters. 实际上, PUT请求应该在Request Body (or payload)上工作,而不是在Request Parameters上工作。 In that sense, servlet API & spring's handling is correct. 从这个意义上说,servlet API和spring的处理是正确的。

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. 话虽如此,更好,更简单的解决方法是从javascript/jQuery调用中传递数据元素,并将您的参数作为url本身的一部分传递。 meaning, set parameters in the url field the way you would do in a GET call. 意思是,在网址字段中设置参数,就像在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. 现在这适用于简单的参数,我猜,它不适用于复杂的JSON类型。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM