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
As the name here suggests, the reason for this error is sending the request with a non-supported method. As we can expect, we can solve this by defining an explicit mapping for PUT in the existing method mapping: @RequestMapping( value = "/employees", produces = "application/json", method = {RequestMethod.
The DELETE HTTP method is used to delete the resource and @DeleteMapping annotation maps the HTTP DELETE requests onto specific handler methods of a Spring controller. You can use this annotation only at the method level. You can use only the @RequestMapping annotation at the class level.
The HTTP DELETE method is used to delete a resource from the server. Unlike GET and HEAD requests, the DELETE requests may change the server state. Sending a message body on a DELETE request might cause some servers to reject the request. But you still can send data to the server using URL parameters.
Return 405 (Method Not Allowed), unless you want to replace every resource in the entire collection of resource - use with caution. 404 (Not Found) - if id not found or invalid and creating new resource is not allowed. Return 405 (Method Not Allowed), unless you want to modify the collection itself..
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With