Spring Boot REST API that consumes the public Random User API and persists data in a PostgreSQL database. Interactive API documentation is available via Swagger UI / OpenAPI.
| Technology | Version |
|---|---|
| Java | 25 |
| Spring Boot | 4.0.3 |
| Spring Web MVC | (managed by Boot) |
| Spring Data JDBC | (managed by Boot) |
| Spring Actuator | (managed by Boot) |
| springdoc-openapi | 3.0.1 |
| PostgreSQL | 15 |
| dotenv-java | 5.2.2 |
| Docker / Docker Compose | - |
| Maven | Wrapper included |
- Java 25+
- Maven (or use the included
./mvnwwrapper) - Docker Desktop
Copy the template and fill in the values:
cp .env.template .env.env content:
POSTGRES_USER={POSTGRES_USER}
POSTGRES_PASSWORD={POSTGRES_PASSWORD}
POSTGRES_DB={POSTGRES_DB}
POSTGRES_PORT={POSTGRES_PORT}
⚠️ The.envfile is git-ignored. Never commit it.
cp src/test/resources/application-test.properties.template src/test/resources/application-test.properties
⚠️ This file is also git-ignored. But for credentials and local overrides, use:
src/main/resources/application-local.properties
spring.application.name=spring_boot_java_random_user
springdoc.swagger-ui.path=/api
spring.datasource.url=jdbc:postgresql://localhost:5432/<database_name>
spring.datasource.driver-class-name=org.postgresql.DriverTo avoid exposing database credentials in source code, create a .env file at the project root.
Then, in src/main/resources/application-local.properties:
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD}Both .env and application-local.properties are in .gitignore (already set).
To activate the local profile in IntelliJ or VS Code, add this environment variable to your run configuration:
SPRING_PROFILES_ACTIVE=local
This way, each developer can use their own credentials without risk of leaking them to GitHub.
⚠️ Common startup error:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver classThis error occurs because the PostgreSQL driver is present in the dependencies but the datasource URL is not configured. Fix: either add the
spring.datasource.*properties above, or exclude the DataSource auto-configuration if no database is needed:spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
docker-compose up -d./mvnw spring-boot:runmvn spring-boot:run./mvnw clean package
java -jar target/spring_boot_java_random_user-0.0.1-SNAPSHOT.jarThe application will be available at: http://localhost:8080
http://localhost:8080/api
Raw OpenAPI specification (JSON):
http://localhost:8080/v3/api-docs
GET /random-users?count=500PUT /random-users/{id}
Content-Type: application/jsonExample body:
{
"gender": "female",
"firstname": "Albert",
"lastname": "Bing",
"civility": "Mrs",
"email": "albert.bing@example.com",
"phone": "123456789",
"picture": "pic.jpg",
"nat": "FR"
}Responses:
200if the user was updated successfully404if the user id does not exist
http://localhost:8080/actuator
http://localhost:8080/actuator/health
The Maven plugin starts and stops Docker Compose automatically during tests:
./mvnw verifyExecution cycle:
pre-integration-test→docker-compose up(PostgreSQL starts)integration-test→ tests runpost-integration-test→docker-compose down(PostgreSQL stops)
Prerequisite: Docker must be installed and running.
A GitHub Actions workflow is configured in .github/workflows/sonar.yaml.
pushon all branchespull_request
- Java 25 setup (Temurin)
- Maven build + tests + SonarQube analysis:
./mvnw clean verify sonar:sonar -Dsonar.qualitygate.wait=true| Secret | Description |
| APPLICATION_TEST_PROPERTIES (Base64-encoded application-test.properties content)
| SONAR_TOKEN | SonarQube authentication token |
| SONAR_HOST_URL | SonarQube instance URL |
| POSTGRES_USER | PostgreSQL user |
| POSTGRES_PASSWORD | PostgreSQL password |
| POSTGRES_DB | Database name |
| POSTGRES_PORT | PostgreSQL port |
GITHUB_TOKENis provided automatically by GitHub Actions.
./mvnw clean verifyGenerated reports:
- HTML:
target/site/jacoco/index.html - XML (used by SonarQube):
target/site/jacoco/jacoco.xml
src/
├── main/
│ ├── java/com/xpeho/spring_boot_java_random_user/
│ │ └── SpringBootJavaRandomUserApplication.java ← Entry point, loads .env
│ └── resources/
│ ├── application.properties ← Spring configuration
│ └── schema.sql ← Database schema
└── test/
├── java/com/xpeho/spring_boot_java_random_user/
│ └── SpringBootJavaRandomUserApplicationTests.java
└── resources/
├── application-test.properties ← Test config (git-ignored)
└── application-test.properties.template ← Template to copy
.env ← Environment variables (git-ignored)
.env.template ← Template to copy
docker-compose.yml ← Local PostgreSQL
The user table is created automatically on startup via schema.sql:
| Column | Type | Description |
|---|---|---|
id |
SERIAL PK | Auto-incremented identifier |
gender |
VARCHAR(20) | Gender |
firstname |
VARCHAR(100) | First name (name.first) |
lastname |
VARCHAR(100) | Last name (name.last) |
civility |
VARCHAR(20) | Title (name.title) |
email |
VARCHAR(255) | Email address |
phone |
VARCHAR(50) | Phone number |
picture |
VARCHAR(500) | Medium picture URL |
nationality |
VARCHAR(10) | Nationality |
This project consumes the public Random User Generator API:
- URL: https://randomuser.me/api/
- Documentation: https://randomuser.me/documentation
- Add Sonarqube in the project
- Add PostgreSQL database with docker
- Add this endpoint get /user/random
- Add this endpoint get /user/{id}
- Add this endpoint put /user/{id}
- Add this endpoint delete /user/{id}
- Add this endpoint post /user
Project developed by XPEHO.