Spring Boot @DeleteMapping Example

1. Introduction

The @DeleteMapping annotation in Spring Boot simplifies the creation of RESTful web services by handling HTTP DELETE requests. It allows developers to define a method in a controller that responds to an HTTP DELETE request, typically used for deleting resources.

Key Points:

1. @DeleteMapping is used to handle the HTTP DELETE request type and map it to a specific handler method.

2. This annotation helps in creating readable and expressive handler methods that clearly state their purpose.

3. It is often used in RESTful APIs to implement the functionality for deleting resources.

2. Implementation Steps

1. Define a Spring Boot application with web dependencies.

2. Create a domain model class if necessary for your application.

3. Set up a REST controller with a method annotated with @DeleteMapping.

4. Provide the URL pattern that the method should map to, including path variables if necessary.

5. Implement the logic for the deletion within the method.

6. Run the Spring Boot application and test the DELETE endpoint.

3. Implementation Example

Here is the complete code that demonstrates the usage of @DeleteMapping annotation:
// Step 3: Create a REST controller with the delete handler method
public class UserController {

    // Map to the URL pattern "/users/{id}" for deletion
    public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
        // Step 5: Normally here you would call a service to delete the user
        // For demonstration, we'll pretend the user is deleted
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);

// Step 6: Create the main Spring Boot application class
public class DeleteMappingExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(DeleteMappingExampleApplication.class, args);


// No body returned, but an HTTP 204 No Content status should be returned to indicate success.


1. The UserController class is annotated with @RestController, indicating it's a controller for a REST API.

2. @DeleteMapping("/users/{id}") specifies that the deleteUser method handles DELETE requests to the "/users/{id}" URL, where {id} is a placeholder for the user's ID.

3. @PathVariable Long id is used to capture the {id} from the URL and make it available as a method parameter.

4. Inside the deleteUser method, there would typically be a service call to delete the user with the given id. Here, we mock the behavior by returning a ResponseEntity<Void> with an HTTP status of NO_CONTENT.

5. DeleteMappingExampleApplication is the main application class with the main method that starts the Spring Boot application.

6. Upon receiving a DELETE request to the endpoint, the server responds with a status code of 204 No Content, indicating that the resource was successfully deleted, or in this case, that the delete operation was simulated.