A small but complete REST API built with Spring Boot, demonstrating a standard CRUD service with database persistence and request validation. Built as a learning project to practise the Spring ecosystem (Spring MVC, Spring Data JPA, Hibernate).
- Java 17
- Spring Boot 3.3 (Spring Web, Spring Data JPA, Validation)
- Hibernate (JPA)
- H2 in-memory database (easily swappable for PostgreSQL)
- Maven
A CRUD REST API over a Task resource:
| Method | Route | Description |
|---|---|---|
| GET | /tasks |
List all tasks |
| GET | /tasks/{id} |
Get a single task (404 if not found) |
| POST | /tasks |
Create a task (validates input) |
| PUT | /tasks/{id} |
Update a task |
| DELETE | /tasks/{id} |
Delete a task (404 if not found) |
A Task has a title (required, max 200 chars), an optional description, and a
done flag. Invalid input — such as an empty title — is rejected with 400 Bad Request.
Requires a JDK (17+) and Maven.
mvn spring-boot:runThe API starts on http://localhost:8080. Alternatively, open the project in
IntelliJ IDEA and run TasksApplication.
The database is H2 in-memory, created on startup and discarded on shutdown — no
setup required. You can browse it at http://localhost:8080/h2-console
(JDBC URL jdbc:h2:mem:tasksdb, user sa, empty password).
# Create a task
curl -X POST http://localhost:8080/tasks \
-H "Content-Type: application/json" \
-d '{"title":"Write documentation","description":"Cover all endpoints"}'
# List tasks
curl http://localhost:8080/tasks
# Update a task
curl -X PUT http://localhost:8080/tasks/1 \
-H "Content-Type: application/json" \
-d '{"title":"Write documentation","done":true}'
# Delete a task
curl -X DELETE http://localhost:8080/tasks/1src/main/java/com/example/tasks/
├── TasksApplication.java # entry point
├── model/Task.java # JPA entity (maps to the task table)
├── repository/TaskRepository.java # Spring Data repository (DB access)
└── controller/TaskController.java # REST controller (the HTTP endpoints)
The layering follows the standard Spring pattern: the controller handles HTTP, the repository handles persistence, and the entity defines the data model. Database schema is generated from the entity by Hibernate.
The code is database-agnostic via JPA. To run against PostgreSQL instead of H2,
swap the H2 dependency in pom.xml for the PostgreSQL driver and update the
datasource settings in application.properties (a commented example is included
there). No Java code changes are required.