![]() |
VOOZH | about |
In Spring Boot, @PutMapping and @DeleteMapping are commonly used in controller classes to map HTTP PUT and DELETE requests to specific methods. These annotations make the code more readable and help implement RESTful CRUD operations efficiently.
@DeleteMapping in Spring is used to handle HTTP DELETE requests in RESTful web services. It maps a specific URL to a controller method that deletes a resource from the server. It is a shortcut for @RequestMapping(method = RequestMethod.DELETE).
Example:
Explanation: the deleteStudent method is annotated with @DeleteMapping. It handles DELETE requests sent to /students/{id}. The student ID is passed as a path variable to delete the specific student.
@PutMapping is used to handle HTTP PUT requests in a REST API. It maps a request to a controller method that updates an existing resource on the server. This annotation is commonly used when a client sends updated data to modify existing records.
Example:
Explanation: @PutMapping("/{id}") handles the HTTP PUT request to update a user's name. The @PathVariable annotation is used to get the user ID from the URL, while @RequestBody receives the new name sent in the request body. The method then processes the data and returns a confirmation message
| Feature | @PutMapping | @DeleteMapping |
|---|---|---|
| Purpose | Used to update an existing resource. | Used to delete an existing resource. |
| HTTP Method | Handles HTTP PUT requests. | Handles HTTP DELETE requests. |
| Data Handling | Usually receives updated data in the request body. | Usually requires only the resource ID to delete it. |
| Example Use | Updating user details, product price, etc. | Deleting a user, product, or record. |
| Example Annotation | @PutMapping("/users/{id}") | @DeleteMapping("/users/{id}") |