23

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

23
  • What mapping do you have on the controller class itself? Do you have an @ApplicationPath set up as well? Commented Apr 28, 2014 at 19:50
  • Thanks! I have this @RequestMapping("/authorization_servers") Commented Apr 28, 2014 at 19:50
  • Moved this to a comment as suggested. The method is called by Ajax, correct? Commented Apr 28, 2014 at 19:51
  • Show more from the log, error message Commented Apr 28, 2014 at 19:51
  • 1
    And what url do you use to access that? I'm sorry to be asking obvious questions, but when we figure this out, I sense it will be a /facepalm moment. Commented Apr 28, 2014 at 20:31

5 Answers 5

18

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.

Sign up to request clarification or add additional context in comments.

5 Comments

groan Can't believe I missed that
This really shouldn't give a 405. If it does, it is a bug with Spring.
it is not working for me :/ stackoverflow.com/questions/11471604/…
You aren't supposed to need the @ResponseBody if your class is a RestController class. Those details weren't in the original question, but I'm having a similar issue, but shouldn't need to add the WResponseBody.
@gaoagong Yeah, that's the way to do it these days. This is an old question!
7

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.

2 Comments

This was helpful, Thanks. Whoever read this, check your brackets !!
Also check for mapping in my case I had 2 controller with mapping like ```/contacts/{id} DELETE`` and "/contacts GET" so when I called my DELETE Endpoint with {id} pathVariable as empty url got matched to my /contacts/ GET endpoint, giving 405
4

Your annotation should look like this:

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

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

3 Comments

OP probably has used static imports for DELETE. But if not, good catch!
Thank you for your time, however I did static import DELETE from spring: import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
Blast! Back to the drawing board.
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.