【问题标题】:Spring - 405 Http method DELETE is not supported by this URLSpring - 此 URL 不支持 405 Http 方法 DELETE
【发布时间】:2014-06-14 12:25:50
【问题描述】:

我在 Spring 中执行“DELETE”HTTP 请求时遇到了一个奇怪的问题。

我有一个控制器方法,我已将 DELETE 请求映射到:

    @RequestMapping(value = "/{authorizationUrl}",method=DELETE)
    public void deleteAuthorizationServer(
            @RequestHeader(value="Authorization") String authorization,
            @PathVariable("authorizationUrl") String authorizationUrl)
            throws  IOException {

        System.out.println("TEST");

    }

控制器使用@RequestMapping("/authorization_servers");映射 当我通过我的 DEV Http 客户端发送请求时,我得到了响应:405 Http method DELETE is not supported by this URL

请求如下所示:

 DELETE    localhost:8080/authorization_servers/asxas

  Headers:
  Authorization: "test:<stuff>"

如果有人可以对此进行调查并帮助我,我将不胜感激

【问题讨论】:

  • 控制器类本身有什么映射?你也设置了@ApplicationPath 吗?
  • 谢谢!我有这个@RequestMapping("/authorization_servers")
  • 按照建议将此移至评论。该方法是由 Ajax 调用的,对吗?
  • 从日志中显示更多,错误信息
  • 你用什么网址来访问它?很抱歉提出明显的问题,但当我们弄清楚这一点时,我觉得这将是一个 /facepalm 时刻。

标签: java spring spring-mvc http-status-code-405 http-delete


【解决方案1】:

这将起作用:

@RequestMapping(value = "/{authorizationUrl}", method = DELETE)
@ResponseBody
public void deleteAuthorizationServer(
    @RequestHeader(value="Authorization") String authorization,
    @PathVariable("authorizationUrl") String authorizationUrl
){
    System.out.printf("Testing: You tried to delete %s using %s\n", authorizationUrl, authorization);
}

你错过了@ResponseBody。您的方法实际上被调用了;正是在那之后发生的事情才产生了错误代码。

【讨论】:

【解决方案2】:

您的注释应如下所示:

@RequestMapping(value = "/{authorizationUrl}",method=RequestMethod.DELETE)

我不知道你从哪里得到那个 DELETE 变量。 :-)

【讨论】:

  • OP 可能已将静态导入用于 DELETE。但如果没有,那就好!
  • 感谢您的宝贵时间,但我从 spring 中进行了静态导入 DELETE:import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
  • 爆炸!回到绘图板。
【解决方案3】:

如果@RequestMapping 模式不匹配或无效,则会导致 404 未找到。但是,如果它碰巧使用不同的方法(例如 GET)匹配另一个映射,则会导致此 405 Http method DELETE is not supported

我的问题就像这个问题一样,除了我的 requestMapping 是原因。是这样的:

@RequestMapping(value = { "/thing/{id:\\d+" }, method = { RequestMethod.DELETE })

你看到了吗?缺少内部右大括号,应该是:{ "/thing/{id:\\d+}" } \\d+ 是匹配 1 个或多个数字的正则表达式。大括号分隔路径中的参数以用于@PathVariable

由于它无效,它无法匹配我的 DELETE 请求: http://example.com/thing/33 这会导致 404 not found 错误,但是,我有另一个 GET 映射:

@RequestMapping(value = { "/thing/{id:\\d+}" }, method = { RequestMethod.GET })

由于大括号模式是正确的,但它不是一个DELETE方法,所以它给出了一个错误405方法不支持。

【讨论】:

  • 这很有帮助,谢谢。谁读过这个,检查你的括号!!
【解决方案4】:

我需要返回ResponseEntity&lt;Void&gt;(带有自定义响应状态),而不是在HttpServletResponse(来自端点方法参数)上设置自定义响应状态。

例如:http://shengwangi.blogspot.com/2016/02/response-for-get-post-put-delete-in-rest.html

【讨论】:

    【解决方案5】:

    还要确保您使用"Content-Type" header="text/html" 调用它。如果不是,则更改它或在requestMapping 中指定它。如果不匹配,您将得到相同的 405。

    【讨论】:

      猜你喜欢
      • 2017-09-12
      • 2011-07-19
      • 2018-04-03
      • 1970-01-01
      • 2023-03-29
      • 2012-12-14
      • 2021-05-21
      相关资源
      最近更新 更多