Build List Tasks
Now that we have our domain model, DTOs, mappers, and repositories set up, let's implement our first endpoint: listing task lists. We'll need to create both the controller and service layers to handle this functionality.
Creating the Service Interface
First, let's create the TaskListService interface in a new package com.devtiro.tasks.services:
package com.devtiro.tasks.services;
import com.devtiro.tasks.domain.entities.TaskList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public interface TaskListService {
List<TaskList> listTaskLists();
}This interface defines our service contract with a single method to retrieve all task lists.
Implementing the Service
Now let's create the implementation in com.devtiro.tasks.services.impl:
package com.devtiro.tasks.services.impl;
import com.devtiro.tasks.domain.entities.TaskList;
import com.devtiro.tasks.repositories.TaskListRepository;
import com.devtiro.tasks.services.TaskListService;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TaskListServiceImpl implements TaskListService {
private final TaskListRepository taskListRepository;
public TaskListServiceImpl(TaskListRepository taskListRepository) {
this.taskListRepository = taskListRepository;
}
@Override
public List<TaskList> listTaskLists() {
return taskListRepository.findAll();
}
}Let's break down the key parts:
@Serviceannotation marks this as a Spring service component- We inject the
TaskListRepositorythrough constructor injection listTaskLists()simply delegates to the repository'sfindAll()method
Creating the Controller
Next, let's create the TaskListController in the com.devtiro.tasks.controllers package:
package com.devtiro.tasks.controllers;
import com.devtiro.tasks.domain.dto.TaskListDto;
import com.devtiro.tasks.mappers.TaskListMapper;
import com.devtiro.tasks.services.TaskListService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(path = "/task-lists")
public class TaskListController {
private final TaskListService taskListService;
private final TaskListMapper taskListMapper;
public TaskListController(TaskListService taskListService, TaskListMapper taskListMapper) {
this.taskListService = taskListService;
this.taskListMapper = taskListMapper;
}
@GetMapping
public List<TaskListDto> listTaskLists() {
return taskListService.listTaskLists()
.stream()
.map(taskListMapper::toDto)
.toList();
}
}Let's examine the important aspects of the controller:
-
Class-level annotations:
@RestControllerindicates this is a REST controller@RequestMapping(path = "/task-lists")sets the base URL path
-
Dependencies:
TaskListServicefor business logicTaskListMapperfor entity-DTO conversion
-
The
listTaskListsmethod:@GetMappingmaps this to HTTP GET requests- Returns a
List<TaskListDto>which Spring will automatically convert to JSON - Uses Java streams to convert entities to DTOs
How It Works Together
When a GET request arrives at /task-lists:
- Spring routes the request to our controller's
listTaskListsmethod - The controller calls
taskListService.listTaskLists() - The service delegates to
taskListRepository.findAll() - The repository returns a list of TaskList entities
- The controller uses the mapper to convert each entity to a DTO
- Spring converts the list of DTOs to JSON and sends it to the client
Testing the Endpoint
Now, if we head over to the frontend we can try out our new endpoint.
That said, there's not a great deal to see right now. In our networks panel we can see calls being made to our backend and returning an empty list.
Summary
- Created the
TaskListServiceinterface and implementation - Implemented the
TaskListControllerwith list functionality - Connected the layers using dependency injection
- Set up entity-to-DTO conversion in the controller
- Created a working GET endpoint for listing task lists