🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101

To-do REST API with Spring Boot

Your first proper REST API: create, read, update and delete tasks with validation, error handling and tests.

Beginner⏱ 6–10 hoursđŸĒœ 8 stepsJavaSpring BootSpring Data JPAH2/MySQL

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. 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. 2

    Entity and repository

    @Entity Task(id, title, description, status, dueDate); interface TaskRepository extends JpaRepository<Task, Long>.

  3. 3

    Service layer

    TaskService holds the logic: create, findAll(status, pageable), update, delete; throw TaskNotFoundException when missing.

  4. 4

    Controller

    @RestController @RequestMapping("/api/tasks") with @GetMapping, @PostMapping, @PutMapping("/{id}"), @DeleteMapping("/{id}"); use DTOs, not the entity, in requests.

  5. 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. 6

    Swagger

    Add springdoc-openapi-starter-webmvc-ui and open /swagger-ui.html to try every endpoint.

  7. 7

    Tests

    Write a @WebMvcTest for the controller with MockMvc and a @DataJpaTest for a custom query.

  8. 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