A comprehensive Spring Boot RESTful API project with 10 fully-featured services demonstrating best practices for enterprise applications.
- Features
- Technology Stack
- Project Structure
- Prerequisites
- Getting Started
- API Documentation
- Available Services
- Testing
- Database
- Configuration
- 10 RESTful Services with full CRUD operations
- Package-by-Feature structure for better modularity
- Comprehensive Validation using Bean Validation API
- Global Exception Handling with custom error responses
- Standard API Response Wrapper for consistent responses
- OpenAPI/Swagger Documentation - fully compatible with Swagger Editor
- H2 In-Memory Database with sample data
- Centralized Logging using SLF4J
- Health Check Endpoint for monitoring
- Unit Tests for service and controller layers
- Proper HTTP Status Codes (200, 201, 400, 404, 500)
- Java 17
- Spring Boot 3.1.5
- Maven - Dependency Management
- Spring Data JPA - Data Access
- H2 Database - In-Memory Database
- Springdoc OpenAPI - API Documentation
- Lombok - Boilerplate Code Reduction
- JUnit 5 - Testing Framework
- Mockito - Mocking Framework
java-spring-boot-api/
βββ src/
β βββ main/
β β βββ java/com/example/springbootapi/
β β β βββ SpringBootApiApplication.java
β β β βββ common/
β β β β βββ config/
β β β β β βββ OpenApiConfig.java
β β β β βββ exception/
β β β β β βββ GlobalExceptionHandler.java
β β β β β βββ ResourceNotFoundException.java
β β β β β βββ BadRequestException.java
β β β β β βββ ErrorDetails.java
β β β β βββ response/
β β β β βββ ApiResponse.java
β β β βββ health/
β β β β βββ HealthCheckController.java
β β β βββ users/
β β β β βββ User.java
β β β β βββ UserRepository.java
β β β β βββ UserService.java
β β β β βββ UserController.java
β β β βββ products/
β β β βββ orders/
β β β βββ customers/
β β β βββ invoices/
β β β βββ payments/
β β β βββ categories/
β β β βββ reviews/
β β β βββ suppliers/
β β β βββ inventory/
β β βββ resources/
β β βββ application.yml
β β βββ data.sql
β βββ test/
β βββ java/com/example/springbootapi/
β βββ users/
β βββ UserServiceTest.java
β βββ UserControllerTest.java
βββ pom.xml
Before you begin, ensure you have the following installed:
- Java 17 or higher
- Maven 3.6+ or higher
- IDE (IntelliJ IDEA, Eclipse, VS Code, or similar)
git clone <repository-url>
cd java-spring-boot-apimvn clean installmvn spring-boot:runOr run the JAR file directly:
java -jar target/spring-boot-api-1.0.0.jarThe application will start on port 8080. You can access:
- API Base URL:
http://localhost:8080/api - Swagger UI:
http://localhost:8080/swagger-ui.html - OpenAPI JSON:
http://localhost:8080/v3/api-docs - H2 Console:
http://localhost:8080/h2-console- JDBC URL:
jdbc:h2:mem:testdb - Username:
sa - Password:
password
- JDBC URL:
Access the interactive API documentation at:
http://localhost:8080/swagger-ui.html
Download the OpenAPI specification in JSON format:
http://localhost:8080/v3/api-docs
You can import this specification into Swagger Editor (https://editor.swagger.io/) for editing and validation.
The API includes the following 10 RESTful services:
- Manage user accounts
- Fields: username, email, fullName, phoneNumber, active
- Manage product catalog
- Fields: name, description, price, stockQuantity, sku, available
- Manage customer orders
- Fields: orderNumber, customerId, totalAmount, status, orderDate, notes
- Manage customer information
- Fields: firstName, lastName, email, phoneNumber, address, city, country
- Manage invoicing
- Fields: invoiceNumber, customerId, orderId, amount, tax, totalAmount, issueDate, dueDate, status
- Manage payment transactions
- Fields: transactionId, orderId, invoiceId, amount, paymentMethod, status, paymentDate
- Manage product categories
- Fields: name, description, slug, parentCategoryId, active, displayOrder
- Manage product reviews
- Fields: productId, userId, rating, title, comment, reviewDate, verified, helpfulCount
- Manage supplier information
- Fields: companyName, contactName, email, phoneNumber, address, city, country, postalCode, active
- Manage inventory items
- Fields: productId, warehouseLocation, quantity, reservedQuantity, reorderLevel, lastUpdated, notes, active
- Check application health status
Each service supports the following operations:
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/{resource} |
Get all items |
| GET | /api/{resource}/{id} |
Get item by ID |
| POST | /api/{resource} |
Create new item |
| PUT | /api/{resource}/{id} |
Update item |
| DELETE | /api/{resource}/{id} |
Delete item |
curl -X POST http://localhost:8080/api/users \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"email": "john@example.com",
"fullName": "John Doe",
"phoneNumber": "+1234567890",
"active": true
}'{
"status": "success",
"message": "User created successfully",
"data": {
"id": 1,
"username": "john_doe",
"email": "john@example.com",
"fullName": "John Doe",
"phoneNumber": "+1234567890",
"active": true
},
"timestamp": "2024-10-12T10:30:00"
}mvn testmvn test -Dtest=UserServiceTestThe project includes unit tests for:
- UserService - Service layer logic
- UserController - REST endpoint behavior
You can extend these tests to other services following the same pattern.
Access the H2 database console at:
http://localhost:8080/h2-console
Connection Details:
- Driver Class:
org.h2.Driver - JDBC URL:
jdbc:h2:mem:testdb - Username:
sa - Password:
password
The application automatically loads sample data on startup from data.sql. This includes:
- 3 Users
- 5 Products
- 3 Customers
- 3 Orders
- 3 Invoices
- 3 Payments
- 4 Categories
- 3 Reviews
- 3 Suppliers
- 5 Inventory Items
The main configuration is in src/main/resources/application.yml:
server:
port: 8080
spring:
datasource:
url: jdbc:h2:mem:testdb
username: sa
password: password
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
springdoc:
swagger-ui:
path: /swagger-ui.htmlTo change the server port, update application.yml:
server:
port: 9090Or pass it as a command-line argument:
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=9090The API uses standardized error responses:
{
"status": "error",
"message": "Validation failed",
"path": "/api/users",
"timestamp": "2024-10-12T10:30:00",
"errors": {
"email": "Email should be valid",
"username": "Username must be between 3 and 50 characters"
}
}{
"status": "error",
"message": "User not found with id: 999",
"path": "/api/users/999",
"timestamp": "2024-10-12T10:30:00"
}{
"status": "error",
"message": "An internal server error occurred",
"path": "/api/users",
"timestamp": "2024-10-12T10:30:00"
}mvn clean packageThe JAR file will be created in target/spring-boot-api-1.0.0.jar
java -jar target/spring-boot-api-1.0.0.jarThe application uses SLF4J with Logback for logging. Log levels can be configured in application.yml:
logging:
level:
root: INFO
com.example.springbootapi: DEBUG- Package-by-Feature - Better modularity and cohesion
- Separation of Concerns - Controller, Service, Repository layers
- Validation - Input validation using Bean Validation
- Exception Handling - Global exception handler for consistent error responses
- API Documentation - Comprehensive OpenAPI/Swagger documentation
- Testing - Unit tests for critical components
- Logging - Structured logging throughout the application
- Response Wrapping - Consistent response format across all endpoints
This project is licensed under the Apache License 2.0.
Contributions are welcome! Please feel free to submit a Pull Request.
For questions or support, please contact: stefanramac@gmail.com
Happy Coding! π