Spring - 405 Http method DELETE is not supported by this URL
Solution 1
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.
Solution 2
Your annotation should look like this:
@RequestMapping(value = "/{authorizationUrl}",method=RequestMethod.DELETE)
I don't know where you got that DELETE variable from. :-)
Solution 3
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.
Solution 4
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
Related videos on Youtube
Sasanka Panguluri
Updated on May 12, 2021Comments
-
Sasanka Panguluri about 3 years
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
-
jgitter about 10 yearsWhat mapping do you have on the controller class itself? Do you have an @ApplicationPath set up as well?
-
Sasanka Panguluri about 10 yearsThanks! I have this
@RequestMapping("/authorization_servers")
-
geoand about 10 yearsMoved this to a comment as suggested. The method is called by Ajax, correct?
-
NimChimpsky about 10 yearsShow more from the log, error message
-
Sasanka Panguluri about 10 years@geoand Nope, it's not an AJAX. It's an ordinary request
-
geoand about 10 years@SasankaPanguluri And what do the caller expect to see in return?
-
jgitter about 10 years@geoand 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.
-
geoand about 10 years@jgitter If you say so :)
-
jgitter about 10 yearsWhat version of spring is this?
-
Karibasappa G C about 10 yearsHi, can you check what method you are passing while making request that is method =? , if not specified specify like method="DELETE" after form action..
-
geoand about 10 yearsWhat happens if you add
required=false
to@RequestHeader
? -
Sasanka Panguluri about 10 years@KaribasappaGC It's DELETE. I just checked it.
-
jgitter about 10 yearsHave 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?
-
Sasanka Panguluri about 10 years@geoand, jgitter I will try it and get back in a couple of moments. Thanks!
-
geoand about 10 yearsAlso, could you post your Spring MVC configuration?
-
Sasanka Panguluri about 10 years@geoand I am not using an xml for the configuration, I have configured it through my code itself.
-
Sasanka Panguluri about 10 years@jgitter I have removed RequestHeader and it still throws the same error on me :(
-
geoand about 10 years@SasankaPanguluri Ok, could you post your Java Config?
-
jgitter about 10 yearsAre there any other mappings in that controller that work? Are you sure there isn't a context root?
-
Sasanka Panguluri about 10 years@jgitter Yes, I have another method that is mapped like this: ` @RequestMapping(method = PUT)` public void ...<stuff>` And it works!
-
jgitter about 10 yearsAnd 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.
-
Sasanka Panguluri about 10 years@jgitter No problem, that is exactly how I trackback too. This is how I am calling the other method that works:
localhost:8080/authorization_servers/
-
Sasanka Panguluri about 10 yearsUpdate: 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.
-
-
geoand about 10 yearsOP probably has used static imports for DELETE. But if not, good catch!
-
Sasanka Panguluri about 10 yearsThank you for your time, however I did static import DELETE from spring:
import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
-
jgitter about 10 yearsBlast! Back to the drawing board.
-
jgitter about 10 yearsgroan Can't believe I missed that
-
Sotirios Delimanolis about 10 yearsThis really shouldn't give a 405. If it does, it is a bug with Spring.
-
mfaisalhyder about 8 yearsit is not working for me :/ stackoverflow.com/questions/11471604/…
-
hamza saber about 4 yearsThis was helpful, Thanks. Whoever read this, check your brackets !!