Vai al contenuto

Getting Started

Requisiti

  • JDK 21+ (Temurin / OpenJDK / GraalVM)
  • Docker (per PostgreSQL e Testcontainers)
  • Maven 3.9+ (oppure usa ./mvnw incluso)

Verifica:

java -version    # >= 21
docker info      # daemon attivo

Setup in 3 minuti

1. Clona il repository

git clone https://github.com/fedcal/Spring-Boot-From-Scratch.git
cd Spring-Boot-From-Scratch

2. Avvia con Docker Compose

docker compose up --build

Lo stack include:

  • app — l'applicazione Spring Boot (port 8080)
  • postgres — PostgreSQL 16 (port 5432)
  • prometheus — Prometheus scraping su /actuator/prometheus (port 9090)

3. Esplora l'API

Apri Swagger UI:

🔗 http://localhost:8080/swagger-ui.html

Oppure usa curl:

# Crea un progetto
curl -s -X POST http://localhost:8080/api/v1/projects \
  -H 'content-type: application/json' \
  -d '{"name":"Demo","description":"Primo progetto"}' | jq

# Crea un task nel progetto (sostituisci $PROJECT_ID)
curl -s -X POST http://localhost:8080/api/v1/projects/$PROJECT_ID/tasks \
  -H 'content-type: application/json' \
  -d '{"title":"Scrivere docs","priority":"HIGH"}' | jq

# Cambia stato del task TODO -> IN_PROGRESS
curl -s -X PATCH http://localhost:8080/api/v1/tasks/$TASK_ID/status \
  -H 'content-type: application/json' \
  -d '{"status":"IN_PROGRESS"}' | jq

Workflow di sviluppo

Avvia solo il database

docker compose up -d postgres

Avvia l'app in modalità dev

./mvnw spring-boot:run

In profilo dev:

  • SQL loggato in console (spring.jpa.show-sql=true)
  • Livello di log applicativo DEBUG
  • Stack trace nelle response di errore

Hot reload

Aggiungi spring-boot-devtools come dipendenza opzionale (non incluso di default per non gonfiare il bundle in produzione).

Eseguire i test

# Tutto (compile + unit + IT con Testcontainers + coverage gate)
./mvnw verify

# Solo unit (no Docker)
./mvnw test -Dtest='!*IT'

# Solo Integration Test
./mvnw verify -Dtest='*IT'

# Report coverage
xdg-open target/site/jacoco/index.html

Testcontainers riusabili

Il base test class abilita withReuse(true). Per attivarlo persistente:

echo "testcontainers.reuse.enable=true" >> ~/.testcontainers.properties

Profili applicazione

Profilo Quando usarlo Differenze rilevanti
dev locale SQL loggato, errori dettagliati, actuator esposto
test test minimi log, Flyway abilitato
prod produzione nessun SQL log, niente stack trace nelle response, actuator ridotto a health/info/prometheus

Attiva un profilo con:

SPRING_PROFILES_ACTIVE=prod ./mvnw spring-boot:run

Configurazione via env

Tutte le proprietà sensibili sono sovrascrivibili via variabili d'ambiente:

Env var Default Descrizione
SPRING_PROFILES_ACTIVE dev Profilo attivo
SPRING_DATASOURCE_URL jdbc:postgresql://localhost:5432/sbfs Connection string
SPRING_DATASOURCE_USERNAME sbfs User DB
SPRING_DATASOURCE_PASSWORD sbfs Password DB
SERVER_PORT 8080 Porta HTTP

Prossimi passi