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:

  1. @Service annotation marks this as a Spring service component
  2. We inject the TaskListRepository through constructor injection
  3. listTaskLists() simply delegates to the repository's findAll() 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:

  1. Class-level annotations:

    • @RestController indicates this is a REST controller
    • @RequestMapping(path = "/task-lists") sets the base URL path
  2. Dependencies:

    • TaskListService for business logic
    • TaskListMapper for entity-DTO conversion
  3. The listTaskLists method:

    • @GetMapping maps 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:

  1. Spring routes the request to our controller's listTaskLists method
  2. The controller calls taskListService.listTaskLists()
  3. The service delegates to taskListRepository.findAll()
  4. The repository returns a list of TaskList entities
  5. The controller uses the mapper to convert each entity to a DTO
  6. 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 TaskListService interface and implementation
  • Implemented the TaskListController with 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
© 2026 Devtiro Ltd. All rights reserved