Вопрос

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

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

    @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.

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

Это было полезно?

Решение

This will work:

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

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

Другие советы

Your annotation should look like this:

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

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

If the @RequestMapping pattern 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.

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

@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.

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

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

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

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top