A modern Spring Boot REST API that consumes the public Random User API and persists data in a PostgreSQL database. Includes interactive API documentation via Swagger UI / OpenAPI.
Features β’ Quick Start β’ API Endpoints β’ Testing β’ Architecture
- Features
- Tech Stack
- Prerequisites
- Quick Start
- Configuration
- Running the Project
- API Documentation
- Testing
- Architecture
- Database Schema
- Monitoring
- Quality & CI/CD
- Troubleshooting
- Todo
- β RESTful API - Fetch and manage random users
- β PostgreSQL Integration - Persist data with Spring Data JDBC
- β
OpenAPI/Swagger - Interactive API documentation at
/api - β Docker Support - Easy setup with Docker Compose
- β Health Monitoring - Spring Actuator endpoints
- β Code Quality - SonarQube integration via GitHub Actions
- β Test Coverage - JUnit 5 + Mockito + JaCoCo
- β BDD Testing - Cucumber support (in progress)
- β Environment Management - dotenv-java for secure configuration
| Component | Version | Purpose |
|---|---|---|
| Java | 25 | Language |
| Spring Boot | 4.0.3 | Framework |
| Spring Web MVC | (auto) | REST API |
| Spring Data JDBC | (auto) | Database access |
| Spring Actuator | (auto) | Monitoring |
| PostgreSQL | 15 | Database |
| H2 | (test) | In-memory DB for tests |
| springdoc-openapi | 3.0.1 | OpenAPI/Swagger |
| dotenv-java | 5.2.2 | Environment config |
| Docker Compose | - | Local infrastructure |
| Maven | Wrapper | Build tool |
| JUnit 5 | (auto) | Testing framework |
| Mockito | (auto) | Mocking |
| JaCoCo | 0.8.14 | Code coverage |
- Java 25+ (or compatible JDK)
- Maven 3.8+ (included wrapper available)
- Docker Desktop (for PostgreSQL)
- Git (for version control)
git clone https://github.com/XPEHO/spring_boot_java_random_user.git
cd spring_boot_java_random_usercp .env.template .env
# Edit .env with your PostgreSQL credentialsdocker-compose up -d./mvnw spring-boot:run- Swagger UI: http://localhost:8080/api
- Health Check: http://localhost:8080/actuator/health
cp .env.template .envContent:
POSTGRES_USER=your_user
POSTGRES_PASSWORD=your_password
POSTGRES_DB=your_database
POSTGRES_PORT=5432For local development with different credentials:
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD}Set SPRING_PROFILES_ACTIVE=local in your IDE run configuration.
H2 in-memory database for unit tests (auto-configured, no setup needed).
application.properties includes:
spring.application.name=spring_boot_java_random_user
spring.datasource.url=jdbc:postgresql://localhost:${POSTGRES_PORT}/${POSTGRES_DB}
spring.datasource.driver-class-name=org.postgresql.Driver
springdoc.swagger-ui.path=/api
randomuser.api.base-url=https://dummyjson.com/# Using Maven wrapper (recommended)
./mvnw spring-boot:run
# Or with Maven installed
mvn spring-boot:run# Build JAR
./mvnw clean package
# Run JAR
java -jar target/spring_boot_java_random_user-0.0.1-SNAPSHOT.jar# Build Docker image
docker build -t xpeho/spring-boot-random-user .
# Run container
docker run -p 8080:8080 --env-file .env xpeho/spring-boot-random-userApplication will be available at: http://localhost:8080
http://localhost:8080/api
http://localhost:8080/v3/api-docs
Fetch and persist random users from the external API.
GET /random-users?count=500Parameters:
count(optional): Number of users to fetch (default: 500, max: 5000)
Response: 200 OK
[
{
"id": 1,
"gender": "male",
"firstname": "John",
"lastname": "Doe",
"civility": "Mr",
"email": "john.doe@example.com",
"phone": "+1 234 567 8900",
"picture": "https://example.com/pic.jpg",
"nat": "US"
}
]Retrieve a specific user by ID.
GET /random-users/1Response: 200 OK or 404 Not Found
Update an existing user's information.
PUT /random-users/1
Content-Type: application/jsonRequest Body:
{
"gender": "female",
"firstname": "Jane",
"lastname": "Smith",
"civility": "Ms",
"email": "jane.smith@example.com",
"phone": "+1 987 654 3210",
"picture": "pic.jpg",
"nat": "FR"
}Responses:
200 OK- User updated successfully404 Not Found- User not found
./mvnw verifyTest execution lifecycle:
- pre-integration-test β Docker Compose starts PostgreSQL
- integration-test β Unit and integration tests run
- post-integration-test β Docker Compose stops
# Unit tests only
./mvnw test
# Integration tests only
./mvnw failsafe:integration-test
# Tests with specific tag (Cucumber)
./mvnw test -Dcucumber.filter.tags="@smoke"./mvnw clean verifyGenerated reports:
- HTML Report:
target/site/jacoco/index.html - XML Report:
target/site/jacoco/jacoco.xml(used by SonarQube)
- Framework: JUnit 5
- Mocking: Mockito
- BDD: Cucumber (planned)
- Coverage: JaCoCo
spring_boot_java_random_user/
βββ src/
β βββ main/
β β βββ java/com/xpeho/spring_boot_java_random_user/
β β β βββ SpringBootJavaRandomUserApplication.java β Entry point
β β β βββ config/ β Spring configuration
β β β βββ data/ β Data layer
β β β β βββ converters/ β DTO/Entity conversion
β β β β βββ models/ β Data models
β β β β βββ services/ β Data services
β β β β βββ sources/ β API & DB sources
β β β βββ domain/ β Business logic
β β β β βββ entities/ β Domain entities
β β β β βββ exceptions/ β Custom exceptions
β β β β βββ services/ β Business services
β β β β βββ usecases/ β Use cases
β β β βββ presentation/ β API layer
β β β βββ controllers/ β REST controllers
β β β βββ handlers/ β Exception handlers
β β βββ resources/
β β βββ application.properties β Main config
β β βββ application-local.properties β Local overrides
β β βββ schema.sql β DB schema
β β βββ static/ β Static assets
β βββ test/
β βββ java/ β Test code
β βββ resources/
β βββ application-test.properties β Test config
β βββ features/ β Cucumber features
βββ .github/
β βββ workflows/
β βββ sonar.yaml β SonarQube CI/CD
β βββ ISSUE_TEMPLATE/ β GitHub templates
βββ docker-compose.yml β Local infrastructure
βββ pom.xml β Maven config
βββ .env.template β Env variables template
βββ .gitignore β Git ignore rules
βββ README.md β This file
Presentation Layer (Controllers)
β
Domain Layer (Use Cases, Services)
β
Data Layer (Repositories, Converters)
β
Database (PostgreSQL)
| Column | Type | Constraints | Description |
|---|---|---|---|
id |
SERIAL | PRIMARY KEY | Auto-incremented identifier |
gender |
VARCHAR(20) | - | Gender |
firstname |
VARCHAR(100) | - | First name |
lastname |
VARCHAR(100) | - | Last name |
civility |
VARCHAR(20) | - | Title (Mr, Ms, Mrs, etc.) |
email |
VARCHAR(255) | - | Email address |
phone |
VARCHAR(50) | - | Phone number |
picture |
VARCHAR(500) | - | Avatar/picture URL |
nat |
VARCHAR(10) | - | Nationality code |
Auto-generated on startup via schema.sql:
DROP TABLE IF EXISTS "users";
CREATE TABLE IF NOT EXISTS "users" (
id SERIAL PRIMARY KEY,
gender VARCHAR(20),
firstname VARCHAR(100),
lastname VARCHAR(100),
civility VARCHAR(20),
email VARCHAR(255),
phone VARCHAR(50),
picture VARCHAR(500),
nationality VARCHAR(10)
);http://localhost:8080/actuator/health
http://localhost:8080/actuator
http://localhost:8080/actuator/health
http://localhost:8080/actuator/metrics
http://localhost:8080/actuator/beans
- JVM metrics
- HTTP metrics
- Database connection pool metrics
./mvnw clean verify sonar:sonarTriggers:
- Push to any branch
- Pull requests
What it does:
- Sets up Java 25
- Runs Maven tests with code coverage
- Uploads results to SonarQube
- Waits for quality gate
Required Secrets:
SONAR_TOKEN- SonarQube authenticationSONAR_HOST_URL- SonarQube instance URLPOSTGRES_USER- DB userPOSTGRES_PASSWORD- DB passwordPOSTGRES_DB- DB namePOSTGRES_PORT- DB port
Error:
Failed to configure a DataSource: 'url' attribute is not specified
and no embedded datasource could be configured.
Solution:
Ensure spring.datasource.url is set in application.properties or environment variables.
Error:
org.postgresql.util.PSQLException: Connection to localhost:5432 refused
Solution:
# Check if Docker is running
docker ps
# Start PostgreSQL
docker-compose up -d
# Verify connection
psql -U postgres -h localhostError:
Circular placeholder reference in application-test.properties
Solution:
Use the corrected application-test.properties with H2 configuration:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.DriverError:
error: release version 25 not supported
Solution:
- Install Java 25 or later
- Update Maven compiler plugin version in
pom.xml
This project integrates with Random User Generator API:
- URL: https://randomuser.me/api/
- Documentation: https://randomuser.me/documentation
- Features:
- Generate random users
- Filter by nationality
- Customize response fields
- Delete endpoint - DELETE /random-users/{id}
- Create endpoint - POST /random-users
- Liquibase database migrations
- Cucumber BDD testing suite
- 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
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
XPEHO - GitHub Organization
- Spring Boot Documentation
- Random User API Docs
- PostgreSQL Documentation
- Docker Documentation
- SonarQube Documentation
Made with β€οΈ by XPEHO
β If you found this helpful, please consider starring the repository!