To-do REST API with Spring Boot
Your first proper REST API: create, read, update and delete tasks with validation, error handling and tests.
What you'll learn
- âControllers, services and repositories
- âJPA entities and H2/MySQL
- âValidation and global error handling
What you'll build
- âCRUD endpoints under /api/tasks
- âValidation errors returned as clean JSON
- âFilter by status and paginate
- âAPI docs with Swagger UI
Step-by-step guide
- 1
Generate the project
On start.spring.io pick Java 21, Spring Web, Spring Data JPA, Validation, H2 Database (and MySQL Driver for later).
- 2
Entity and repository
@Entity Task(id, title, description, status, dueDate); interface TaskRepository extends JpaRepository<Task, Long>.
- 3
Service layer
TaskService holds the logic: create, findAll(status, pageable), update, delete; throw TaskNotFoundException when missing.
- 4
Controller
@RestController @RequestMapping("/api/tasks") with @GetMapping, @PostMapping, @PutMapping("/{id}"), @DeleteMapping("/{id}"); use DTOs, not the entity, in requests.
- 5
Validation and errors
Add @NotBlank/@Size on the DTO and @Valid in the controller; a @RestControllerAdvice turns exceptions into JSON with the right status (400, 404).
- 6
Swagger
Add springdoc-openapi-starter-webmvc-ui and open /swagger-ui.html to try every endpoint.
- 7
Tests
Write a @WebMvcTest for the controller with MockMvc and a @DataJpaTest for a custom query.
- 8
Switch to MySQL
Set spring.datasource.url, username and password in application.properties and run with spring.jpa.hibernate.ddl-auto=update.
đ Make it stand out
- +Add users with Spring Security and JWT
- +Dockerise the app and database with docker compose
- +Deploy to a free cloud tier