【问题标题】:getting 400 Bad request with Spring with rest休息时使用 Spring 收到 400 错误请求
【发布时间】:2013-11-14 16:32:20
【问题描述】:

我正在尝试配置一个基于 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 控制器中的 @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 请求,一切正常。

【问题讨论】:

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


    【解决方案1】:

    您的 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);
    }
    

    还要确保在 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 方法。

    【讨论】:

      【解决方案2】:

      查看你的 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 是相当普遍的做法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-18
        • 1970-01-01
        • 2014-07-04
        • 1970-01-01
        • 1970-01-01
        • 2016-09-17
        • 1970-01-01
        相关资源
        最近更新 更多