Java Spring - 此 URL 不支持 405 Http 方法 DELETE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23349871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:07:57  来源:igfitidea点击:

Spring - 405 Http method DELETE is not supported by this URL

javaspringspring-mvchttp-status-code-405http-delete

提问by Sasanka Panguluri

Well I have a strange problem with executing a "DELETE" HTTP request in Spring.

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

I have a controller method which I have mapped a DELETE request to:

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

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

        System.out.println("TEST");

    }

The controller is mapped using @RequestMapping("/authorization_servers");When I send a request through my DEV Http Client, I am getting the response : 405 Http method DELETE is not supported by this URL.

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

The request looks like this:

请求如下所示:

 DELETE    localhost:8080/authorization_servers/asxas

  Headers:
  Authorization: "test:<stuff>"

If someone can look into this and help me, I would be grateful

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

采纳答案by Rob Worsnop

This will work:

这将起作用:

@RequestMapping(value = "/{authorizationUrl}", method = DELETE)
public @ResponseBody 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);
}

You were missing @ResponseBody. Your method was actually getting called; it was what happened after that that was producing the error code.

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

回答by jgitter

Your annotation should look like this:

您的注释应如下所示:

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

I don't know where you got that DELETE variable from. :-)

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

回答by David Lotts

If the @RequestMappingpattern doesn't match or is invalid, it results in a 404 not found. However, if it happens to match another mapping with a different method (ex. GET), it results in this 405 Http method DELETE is not supported.

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

My issue was just like this one, except my requestMapping was the cause. It was this:

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

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

Do you see it? The inner closing brace is missing, it should be: { "/thing/{id:\\d+}" }The \\d+is a regular expression to match 1 or more numeric digits. The braces delimit the parameter in the path for use with @PathVariable.

你看到了吗?缺少内部右大括号,应该是:{ "/thing/{id:\\d+}" }The \\d+is a regular expression to match 1 or more numeric numbers。大括号分隔路径中的参数以用于@PathVariable.

Since it's invalid it can't match my DELETE request: http://example.com/thing/33which would have resulted in a 404 not found error, however, I had another mapping for GET:

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

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

Since the brace pattern is correct, but it's not a method DELETE, then it gave a error 405 method not supported.

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

Hope this helps you!

希望这对你有帮助!

回答by Jason

also make sure you're calling it with "Content-Type" header="text/html". If not, then change it or specify it in the requestMapping. If it doesn't match, you get the same 405.

还要确保你用"Content-Type" header="text/html". 如果没有,则更改它或在requestMapping. 如果不匹配,您会得到相同的 405。

Hope this helps someone.

希望这可以帮助某人。

回答by cellepo

I needed to return ResponseEntity<Void>(with custom response status) instead of setting custom response status on HttpServletResponse(from endpoint method param).

我需要返回ResponseEntity<Void>(使用自定义响应状态)而不是设置自定义响应状态HttpServletResponse(来自端点方法参数)。

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

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