@DeleteMapping Spring Boot Example

Spring @DeleteMapping example shows how to use @DeleteMapping annotation to map HTTP DELETE requests onto specific handler methods.

@DeleteMapping Overview

@DeleteMapping annotation for mapping HTTP DELETE requests onto specific handler methods.
Specifically, @DeleteMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.DELETE).

@DeleteMapping Example

The following example shows how to use @DeleteMapping annotation to map "/employees/{id}" HTTP DELETE requests onto specific handler method - deleteExmployee:
@DeleteMapping("/employees/{id}")
public Map<String, Boolean> deleteEmployee(@PathVariable(value = "id") Long employeeId)
  throws ResourceNotFoundException {
     Employee employee = employeeRepository.findById(employeeId)
       .orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));

     employeeRepository.delete(employee);
     Map<String, Boolean> response = new HashMap<>();
     response.put("deleted", Boolean.TRUE);
     return response;
}

Here is one more example to handle a delete user request using @DeleteMapping annotation:
 @DeleteMapping("/user/{id}")
   public Map<String, Boolean> deleteUser(
       @PathVariable(value = "id") Long userId) throws Exception {
       User user = userRepository.findById(userId)
          .orElseThrow(() -> new ResourceNotFoundException("User not found on :: "+ userId));

       userRepository.delete(user);
       Map<String, Boolean> response = new HashMap<>();
       response.put("deleted", Boolean.TRUE);
       return response;
   }

Comments