Delete Task Endpoint
Let's implement our last REST API endpoint, the delete task endpoint!
Implement the Delete Task Endpoint
The TaskController class is almost complete, just one last endpoint to add:
/**
* Deletes the specified Task.
* Always returns an HTTP 204 NO CONTENT.
*
* @param taskId The ID of the task.
* @return An HTTP 204 NO CONTENT.
*/
@DeleteMapping(path = "/{taskId}")
public ResponseEntity<Void> deleteTask(@PathVariable UUID taskId) {
// Call the deleteTask method on the TaskService.
taskService.deleteTask(taskId);
// Return an HTTP 204 NO CONTENT.
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}Note the Void in the ResponseEntity. This is used when there is no response
body, which is exactly what we expect for a HTTP 204 NO CONTENT.
This endpoint expects the task ID in the URL path. The framework extracts the
task ID from the URL path and passes it to the deleteTask method.
Let's try out the delete task endpoint in the UI!
Summary
- Implemented the
deleteTaskmethod on theTaskControllerclass.