
1. Overview
In this article, we will discuss the @DeleteMapping annotation of the Spring Boot along with an example.
The HTTP DELETE method is?used to delete a specified 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.
2. @DeleteMapping
The @RequestMapping annotation is one of the most widely used annotations that allows you to map web requests to Spring controller methods. This @DeleteMapping is a specialized version that acts as a shortcut for @RequestMapping(method = RequestMethod.DELETE).
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.
For example, the following code contains the deleteOrder
handler method inside the Spring controller. We annotated this method with @DeleteMapping.
@DeleteMapping("/deleteOrder/{orderId}") public Boolean deleteOrder(@PathVariable(value = "id") Long orderId) { return true; }
Whenever the client invokes the below method HTTP://localhost:8080/deleteOrder/12098, we would delete the particular order 12098 from the database.
@DeleteMapping("/deleteOrder/{orderId}") public ResponseEntity<Long> deleteOrder(@PathVariable(value = "id") Long orderId) { return ResponseEntity.ok(orderId); }
You can send the following response status code when a?DELETE
?is successful:
- A?
202
?(Accepted
) status code – the action may probably succeed but has not yet started. - A?
204
?(No Content
) status code – the action completed and no further information is to be supplied. - A?
200
?(OK
) status code – the action completed and the response message includes a representation describing the status.
3. Conclusion
To sum up, we have learned the @DeleteMapping annotation of the Spring boot along with an example. It simply maps the HTTP Delete requests to the controller’s handler method. You can find the code samples of this article in our GitHub repository.