Spring - 405 此 URL 不支持 HTTP 方法 DELETE

好吧,我在春季执行“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");

    }

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

请求如下所示:

 DELETE    localhost:8080/authorization_servers/asxas

  Headers:
  Authorization: "test:<stuff>"

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


答案 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变量。:-)