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

5

24

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

Suspension answered 28/4, 2014 at 19:46 Comment(23)
What mapping do you have on the controller class itself? Do you have an @ApplicationPath set up as well?Hypotrachelium
Thanks! I have this @RequestMapping("/authorization_servers")Suspension
Moved this to a comment as suggested. The method is called by Ajax, correct?Nightshade
Show more from the log, error messagePleo
@Nightshade Nope, it's not an AJAX. It's an ordinary requestSuspension
@SasankaPanguluri And what do the caller expect to see in return?Nightshade
@Nightshade I think you're getting off topic a little bit. If it's not mapping to the correct url, the problem lies with the mapping, not the return type.Hypotrachelium
@Hypotrachelium If you say so :)Nightshade
What version of spring is this?Hypotrachelium
Hi, can you check what method you are passing while making request that is method =? , if not specified specify like method="DELETE" after form action..Dumbarton
What happens if you add required=false to @RequestHeader?Nightshade
@KaribasappaGC It's DELETE. I just checked it.Suspension
Have you tried removing the @RequestHeader parameter? Just curious as it may help to diagnose the issue.... Also, are you getting any errors in the log on startup?Hypotrachelium
@geoand, jgitter I will try it and get back in a couple of moments. Thanks!Suspension
Also, could you post your Spring MVC configuration?Nightshade
@Nightshade I am not using an xml for the configuration, I have configured it through my code itself.Suspension
@Hypotrachelium I have removed RequestHeader and it still throws the same error on me :(Suspension
@SasankaPanguluri Ok, could you post your Java Config?Nightshade
Are there any other mappings in that controller that work? Are you sure there isn't a context root?Hypotrachelium
@Hypotrachelium Yes, I have another method that is mapped like this: ` @RequestMapping(method = PUT)` public void ...<stuff>` And it works!Suspension
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.Hypotrachelium
@Hypotrachelium No problem, that is exactly how I trackback too. This is how I am calling the other method that works: localhost:8080/authorization_servers/Suspension
Update: I got rid of the value="/{authorizationUrl}" attribute to test if it even works straight with the /authorization_servers mapping. No wonder, it still doesn't.Suspension
D
19

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.

Desalinate answered 28/4, 2014 at 20:38 Comment(3)
groan Can't believe I missed thatHypotrachelium
This really shouldn't give a 405. If it does, it is a bug with Spring.Choriamb
it is not working for me :/ #11472104Shull
V
6

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.

Vankirk answered 10/10, 2014 at 3:32 Comment(2)
This was helpful, Thanks. Whoever read this, check your brackets !!Carolincarolina
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 405Prentiss
H
4

Your annotation should look like this:

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

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

Hypotrachelium answered 28/4, 2014 at 19:59 Comment(3)
OP probably has used static imports for DELETE. But if not, good catch!Nightshade
Thank you for your time, however I did static import DELETE from spring: import static org.springframework.web.bind.annotation.RequestMethod.DELETE;Suspension
Blast! Back to the drawing board.Hypotrachelium
T
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

Tinny answered 16/3, 2020 at 2:25 Comment(0)
C
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.

Coenurus answered 26/11, 2015 at 4:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.