List Task Endpoint
Let's add a list tasks endpoint to our REST API!
Implement the List Task Endpoint
The list tasks endpoint is simpler than the create tasks endpoint:
/**
* Lists all tasks with an HTTP 200 OK.
*
* @return The list of tasks.
*/
@GetMapping
public ResponseEntity<List<TaskDto>> listTasks() {
// Call the listTasks method on the TaskService to get a list of tasks.
List<Task> tasks = taskService.listTasks();
// Map the list of Task objects to a list of TaskDto objects.
List<TaskDto> taskDtoList = tasks.stream()
.map(taskMapper::toDto)
.toList();
// Return the list of TaskDto objects with an HTTP 200 OK.
return ResponseEntity.ok(taskDtoList);
}We use @GetMapping as this endpoint uses HTTP GET, and as we're using
HTTP GET we don't expect a request body.
There we have it, the list tasks endpoint is now ready to test in the UI!
Summary
- Implemented the
listTasksmethod on theTaskController.