在Spring休息时获得400 Bad请求

时间:2013-11-14 16:32:20

标签: java spring rest extjs http-status-code-400

我正在尝试基于extjs asfrontend和Spring作为后端来配置一个小的应用程序。因此,要从extjs与服务器通信,我们需要在商店配置代理编写器。我的代理配置如下:

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'
    }
}

这是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每次都会回答“400 Bad request”的描述“客户端发送的请求在语法上是不正确的。”

所以,但如果我将代理类型更改为ajax并将requestMapping更改为获取POST请求,则一切正常。

2 个答案:

答案 0 :(得分:2)

看起来您path variable的语法不正确:

应该是

@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);
}

还要确保在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)

该行确认已注册该方法以处理此URL的DELETE方法。

答案 1 :(得分:0)

查看您的Tomcat / conf / web.xml您将看到如下条目:

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

基本上,在您的服务器上允许put和delete命令通常是不好的做法,因为这使任何人都能够做恶意事情。 Tomcat默认通过关闭它们来保护您。

对于休息服务,将put / deletes映射到Post

是很常见的做法