0

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

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 请求,一切正常。

4

2 回答 2

2

看起来您的路径变量语法不正确:

它应该是这样的

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

还要确保在 web 应用程序启动中看到类似的行

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 方法。

于 2014-04-26T20:15:24.100 回答
0

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

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

本质上,允许在服务器上放置和删除命令通常是不好的做法,因为这使任何人都有能力做恶意的事情。默认情况下,Tomcat 通过关闭这些来保护您。

对于 restful 服务,将 put/delete 映射到 Post 是相当普遍的做法

于 2013-11-14T17:57:50.053 回答