This project is a Kotlin-based Spring Boot application that demonstrates data validation, structured logging and database migrations with Flyway.
- Spring Boot 3 & Kotlin: Main entry point is
DemoApplication.kt. - Persistence: JPA/Hibernate with a PostgreSQL database. Database schema migrations are managed by Flyway. The initial migration creates the
examplestable atV1__create_examples_table.sql. - DTO & Validation:
ExampleDtouses validation groups (OnCreate,OnUpdate) to enforce different rules on create vs update requests. - Layers:
ExampleControllerexposes CRUD endpoints under/examples.ExampleServicecontains business logic.ExampleRepositoryextendsJpaRepositoryfor data access.
- Directory Structure: Example of how these components fit together
src/main/kotlin/com/example/demo
├── controller
│ └── ExampleController.kt
├── dto
│ └── ExampleDto.kt
├── entity
│ └── ExampleEntity.kt
├── mapper
│ └── ExampleMapper.kt
├── repository
│ └── ExampleRepository.kt
└── service
└── ExampleService.kt
- Exception Handling:
GlobalExceptionHandlertranslates exceptions to HTTP responses. - Logging:
Loggablebase class andStructuredLoggingJsonLayoutenable structured JSON logs. - API Docs:
SwaggerConfigsets up OpenAPI documentation.
Tests run using an in-memory H2 database (see src/test/resources/application.yaml).
Run both the application and the PostgreSQL database as containers:
docker compose upThe API will then be available on http://localhost:8080.
Swagger docs are made available at the /swagger-ui.html endpoint
You can start only the database container and run the Spring Boot app via Gradle on your host:
# start postgres only
docker compose up db
# in another terminal, run the application
./gradlew bootRunBy default the app expects PostgreSQL to be accessible on localhost:5432 with the username and password demo. These values can be overridden through environment variables defined in application.yaml.
Execute the full test suite using the Gradle wrapper:
./gradlew test --no-daemon --console=plainThis will spin up an in-memory H2 database and apply Flyway migrations automatically.