Educational project: REST API for managing books built with Spring Boot 4 and PostgreSQL.
- Building REST API with Spring Boot
- Working with databases using Spring Data JPA
- Data validation
- Error handling (RFC 7807 Problem Details)
- Basic Authentication
- Swagger/OpenAPI documentation
- Docker for database
- Writing tests
Before starting, make sure you have installed:
- Java 17+ — check:
java -version - Maven 3.8+ — check:
mvn -version - Docker — check:
docker --version
git clone https://github.com/OKaluzny/spring-boot-rest-api-postgresql.git
cd spring-boot-rest-api-postgresqldocker-compose up -d postgresVerify the container is running:
docker psYou should see container book-db with status Up.
mvn spring-boot:runWait for the message:
Started Application in X seconds
Open in browser: http://localhost:8080/swagger-ui.html
Here you can interactively test the API.
API requires authentication:
- Username:
user - Password:
user
In Swagger UI click the "Authorize" button and enter these credentials.
Create a book:
curl -X POST http://localhost:8080/api/books \
-H "Content-Type: application/json" \
-u user:user \
-d '{"name": "Java Programming", "description": "Learn Java", "tags": ["java"]}'Response:
{
"id": 1,
"name": "Java Programming",
"description": "Learn Java",
"tags": ["java"]
}Get all books:
curl http://localhost:8080/api/books -u user:user| Method | URL | Description |
|---|---|---|
POST |
/api/books |
Create a book |
GET |
/api/books |
Get all books (paginated) |
GET |
/api/books/{id} |
Get book by ID |
GET |
/api/books?name=Java |
Search by name |
PUT |
/api/books/{id} |
Update a book |
DELETE |
/api/books/{id} |
Delete a book |
GET /api/books?page=0&size=10&sort=name,asc
page— page number (starting from 0)size— number of items per pagesort— sorting (field,direction)
src/main/java/com/kaluzny/
│
├── Application.java # Entry point
│
├── config/ # Configuration
│ ├── SecurityConfig.java # Security settings
│ └── OpenApiConfig.java # Swagger settings
│
├── domain/ # Data layer
│ ├── Book.java # JPA entity
│ └── BookRepository.java # Repository
│
├── dto/ # Data Transfer Objects
│ ├── BookCreateRequest.java
│ ├── BookUpdateRequest.java
│ ├── BookResponse.java
│ └── BookMapper.java
│
├── service/ # Business logic
│ └── BookService.java
│
├── exception/ # Error handling
│ ├── BookNotFoundException.java
│ └── GlobalExceptionHandler.java
│
└── web/ # REST controllers
└── BookRestController.java
HTTP Request
↓
┌─────────────────┐
│ Controller │ ← Receives DTO, validation
└────────┬────────┘
↓
┌─────────────────┐
│ Service │ ← Business logic
└────────┬────────┘
↓
┌─────────────────┐
│ Repository │ ← Database operations
└────────┬────────┘
↓
┌─────────────────┐
│ PostgreSQL │
└─────────────────┘
- Stop Spring Boot:
Ctrl+Cin terminal - Stop PostgreSQL:
docker-compose downmvn testTests use H2 in-memory database, PostgreSQL is not required.
| URL | Description |
|---|---|
| http://localhost:8080/swagger-ui.html | Swagger UI |
| http://localhost:8080/v3/api-docs | OpenAPI JSON |
| http://localhost:8080/actuator/health | Health check |
Find and stop the process:
lsof -i :8080
kill -9 <PID>Check that container is running:
docker ps
docker-compose up -d postgresInstall Docker Desktop: https://www.docker.com/products/docker-desktop
| Technology | Version | Description |
|---|---|---|
| Java | 17 | Programming language |
| Spring Boot | 4.0.2 | Framework |
| Spring Data JPA | - | ORM for database |
| Spring Security | - | Security |
| PostgreSQL | 16 | Database |
| Swagger/OpenAPI | 3.1 | API documentation |
| Lombok | 1.18 | Reduce boilerplate |
| JUnit 5 | - | Testing |
MIT