简体   繁体   English

在Spring休息时获得400 Bad请求

[英]getting 400 Bad request with Spring with rest

I'm trying to configure a small aplication based on extjs asfrontend and Spring as backend. 我正在尝试配置基于extjs asfrontend和Spring作为后端的小应用程序。 So to communicate with the server from extjs we need to configure proxy writer at store. 因此,要从extjs与服务器通信,我们需要在商店配置代理编写器。 My proxy configuration looks as follows: 我的代理配置如下:

proxy : {

    type: 'rest',

    reader: {
        type: 'json',
        idProperty: 'id',

        root: 'data',
        totalProperty: 'total',
        successProperty: 'success',
        messageProperty: 'message'
    },

    writer: {
        type: 'json',
        allowSingle: true,
        writeAllFields: true,
        root: 'data'
    },

    api: {
        read:       'countries',
        create:     'country/add',
        update:     'country/update',
        destroy:    'country/delete'
    }
}

And here is the @RequestMapping in a Spring Controller: 这是Spring Controller中的@RequestMapping:

@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = COUNTRY_PATH + DELETE_PATH + SLASH + DELETE_ID_PARAM, method = RequestMethod.DELETE)
public void delete(@PathVariable(DELETE_ID_PARAM) Integer deleteId, @RequestParam Object data)  {
    System.out.println(data.toString());
    countryService.deleteCountry(deleteId);
}

Tomcat answers everytime "400 Bad request" with description "The request sent by the client was syntactically incorrect." Tomcat每次都会回答“400 Bad request”的描述“客户端发送的请求在语法上是不正确的”。

So, but if I change proxy type to ajax and requestMapping to getting POST requests everything works fine. 所以,但如果我将代理类型更改为ajax并将requestMapping更改为获取POST请求,则一切正常。

It looks like you have incorrect syntax for the path variable : 看起来你的路径变量语法不正确:

It should be something like 应该是这样的

@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = COUNTRY_PATH + DELETE_PATH + SLASH + "{" + DELETE_ID_PARAM + "}", method = RequestMethod.DELETE)
public void delete(@PathVariable(DELETE_ID_PARAM) Integer deleteId, @RequestParam  Object data)  {
    System.out.println(data.toString());
    countryService.deleteCountry(deleteId);
}

Also make sure that in the webapplication startup you see a line like 还要确保在webapplication启动中看到类似的行

INFO   [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-4) Mapped "{[/myapp/country/delete],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto   public void com.myorg.web.MyController.delete(@PathVariable(DELETE_ID_PARAM) Integer deleteId, @RequestParam  Object data)

The line confirms that the method is registered to handle DELETE methods for this url. 该行确认已注册该方法以处理此URL的DELETE方法。

Look at your Tomcat/conf/web.xml You'll see an entry like this: 查看您的Tomcat / conf / web.xml您将看到如下条目:

- <!--    readonly            Is this context "read only", so HTTP           --> 
- <!--                        commands like PUT and DELETE are               --> 
- <!--                        rejected?  [true]        

Essentially it's typically bad practice to allow put and delete commands on your server since this gives anyone the ability to do malicious things. 基本上,在您的服务器上允许put和delete命令通常是不好的做法,因为这使得任何人都能够做恶意事情。 Tomcat by default protects you by turnning off those. Tomcat默认通过关闭它们来保护您。

For restful services it is fairly common practice to map puts/deletes to Post 对于休息服务,将put / deletes映射到Post是相当普遍的做法

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

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