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

59,542

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

Share:
59,542

Related videos on Youtube

Sasanka Panguluri
Author by

Sasanka Panguluri

Updated on May 12, 2021

Comments

  • Sasanka Panguluri
    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
      jgitter about 10 years
      What mapping do you have on the controller class itself? Do you have an @ApplicationPath set up as well?
    • Sasanka Panguluri
      Sasanka Panguluri about 10 years
      Thanks! I have this @RequestMapping("/authorization_servers")
    • geoand
      geoand about 10 years
      Moved this to a comment as suggested. The method is called by Ajax, correct?
    • NimChimpsky
      NimChimpsky about 10 years
      Show more from the log, error message
    • Sasanka Panguluri
      Sasanka Panguluri about 10 years
      @geoand Nope, it's not an AJAX. It's an ordinary request
    • geoand
      geoand about 10 years
      @SasankaPanguluri And what do the caller expect to see in return?
    • jgitter
      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
      geoand about 10 years
      @jgitter If you say so :)
    • jgitter
      jgitter about 10 years
      What version of spring is this?
    • Karibasappa G C
      Karibasappa G C about 10 years
      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..
    • geoand
      geoand about 10 years
      What happens if you add required=false to @RequestHeader?
    • Sasanka Panguluri
      Sasanka Panguluri about 10 years
      @KaribasappaGC It's DELETE. I just checked it.
    • jgitter
      jgitter about 10 years
      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?
    • Sasanka Panguluri
      Sasanka Panguluri about 10 years
      @geoand, jgitter I will try it and get back in a couple of moments. Thanks!
    • geoand
      geoand about 10 years
      Also, could you post your Spring MVC configuration?
    • Sasanka Panguluri
      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
      Sasanka Panguluri about 10 years
      @jgitter I have removed RequestHeader and it still throws the same error on me :(
    • geoand
      geoand about 10 years
      @SasankaPanguluri Ok, could you post your Java Config?
    • jgitter
      jgitter about 10 years
      Are there any other mappings in that controller that work? Are you sure there isn't a context root?
    • Sasanka Panguluri
      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
      jgitter about 10 years
      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.
    • Sasanka Panguluri
      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
      Sasanka Panguluri about 10 years
      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.
  • geoand
    geoand about 10 years
    OP probably has used static imports for DELETE. But if not, good catch!
  • Sasanka Panguluri
    Sasanka Panguluri about 10 years
    Thank you for your time, however I did static import DELETE from spring: import static org.springframework.web.bind.annotation.RequestMethod.DELETE‌​;
  • jgitter
    jgitter about 10 years
    Blast! Back to the drawing board.
  • jgitter
    jgitter about 10 years
    groan Can't believe I missed that
  • Sotirios Delimanolis
    Sotirios Delimanolis about 10 years
    This really shouldn't give a 405. If it does, it is a bug with Spring.
  • mfaisalhyder
    mfaisalhyder about 8 years
    it is not working for me :/ stackoverflow.com/questions/11471604/…
  • hamza saber
    hamza saber about 4 years
    This was helpful, Thanks. Whoever read this, check your brackets !!