A RESTful web service built with Spring Boot 3.4.0, Java 21, and MySQL, featuring person management with API versioning, custom mappers, and comprehensive exception handling.
- RESTful API: Full CRUD operations for person management
- API Versioning: Multiple API versions (v1 and v2) for backward compatibility
- Object Mapping: DozerMapper integration and custom mappers for DTO conversion
- Database Integration: MySQL database with JPA/Hibernate
- Exception Handling: Custom exception responses and resource not found handling
- Testing: Unit tests with mocks and comprehensive test coverage
- Logging: Configurable logging levels for debugging and monitoring
- Java 21
- Spring Boot 3.4.0
- Spring Data JPA
- MySQL
- DozerMapper 7.0.0
- Maven
- JUnit 5 (for testing)
src/
βββ main/
β βββ java/br/com/campos/
β β βββ controller/ # REST controllers
β β β βββ PersonController.java
β β β βββ TestLogController.java
β β βββ data/dto/ # Data Transfer Objects
β β β βββ v1/PersonDTO.java
β β β βββ v2/PersonDTOV2.java
β β βββ exception/ # Exception handling
β β β βββ ExceptionResponse.java
β β β βββ ResourceNotFoundException.java
β β β βββ handler/CustomEntityResponseHandler.java
β β βββ mapper/ # Object mapping utilities
β β β βββ ObjectMapper.java
β β β βββ custom/PersonMapper.java
β β βββ model/ # JPA entities
β β β βββ Person.java
β β βββ repository/ # Data access layer
β β β βββ PersonRepository.java
β β βββ services/ # Business logic layer
β β β βββ PersonServices.java
β β βββ converters/ # Utility converters
β β β βββ NumberConverter.java
β β βββ Startup.java # Main application class
β βββ resources/
β βββ application.yml # Application configuration
βββ test/
βββ java/br/com/campos/
βββ unitTests/mapper/ # Unit tests for mappers
βββ StartupTests.java # Integration tests
- Java 21 or higher
- Maven 3.6+
- MySQL 8.0+
Set the following environment variables before running the application:
DATABASE_URL=jdbc:mysql://localhost:3306/your_database
DATABASE_USERNAME=your_username
DATABASE_PASSWORD=your_password-
Clone the repository
git clone <repository-url> cd spring-boot-and-java
-
Build the project
mvn clean install
-
Run the application
mvn spring-boot:run
The application will start on http://localhost:8080
| Method | Endpoint | Description | Request Body | Response |
|---|---|---|---|---|
| GET | /person/{id} |
Get person by ID | - | PersonDTO |
| GET | /person |
Get all persons | - | List<PersonDTO> |
| POST | /person |
Create new person (v1) | PersonDTO | PersonDTO |
| POST | /person/v2 |
Create new person (v2) | PersonDTOV2 | PersonDTOV2 |
| PUT | /person |
Update existing person | PersonDTO | PersonDTO |
| DELETE | /person/{id} |
Delete person by ID | - | 204 No Content |
The API supports multiple versions:
- v1: Standard PersonDTO with basic fields
- v2: Enhanced PersonDTOV2 with additional fields and features
POST /person
Content-Type: application/json
{
"firstName": "John",
"lastName": "Doe",
"address": "123 Main St",
"gender": "Male"
}POST /person/v2
Content-Type: application/json
{
"firstName": "Jane",
"lastName": "Smith",
"address": "456 Oak Ave",
"gender": "Female"
// Additional v2-specific fields
}CREATE TABLE person (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(80) NOT NULL,
last_name VARCHAR(80) NOT NULL,
address VARCHAR(100) NOT NULL,
gender VARCHAR(6) NOT NULL
);spring:
application:
name: rest-with-spring-boot-and-java
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: ${DATABASE_URL}
username: ${DATABASE_USERNAME}
password: ${DATABASE_PASSWORD}
jpa:
hibernate:
ddl-auto: update
show-sql: false
open-in-view: false
logging:
level:
br.com.campos: DEBUG# Run all tests
mvn test
# Run specific test class
mvn test -Dtest=ObjectMapperTests- Unit Tests: Located in
src/test/java/br/com/campos/unitTests/ - Mock Data: Utility classes for generating test data
- Integration Tests: End-to-end testing of the application
- Controller Layer: Handles HTTP requests and responses
- Service Layer: Contains business logic
- Repository Layer: Data access and persistence
- Model Layer: JPA entities representing database tables
- DTO Pattern: Separate data transfer objects for API communication
- Repository Pattern: Abstracted data access layer
- Custom Mapper Pattern: Specialized object mapping for complex transformations
- Exception Handling Pattern: Centralized error handling and response formatting
The application uses two mapping approaches:
- DozerMapper: Generic object mapping utility
- Custom Mappers: Specialized mappers for complex object transformations (e.g., PersonMapper)
Configurable logging levels:
- DEBUG: Detailed application flow (br.com.campos package)
- INFO: General application information
- WARN/ERROR: Issues and exceptions
This project is currently in active development. Features being worked on:
- Authentication and Authorization
- API Documentation (Swagger/OpenAPI)
- Pagination and Sorting
- Validation Enhancements
- Caching Implementation
- Performance Monitoring
- Docker Support
- CI/CD Pipeline
This project is still under development. Contributions, suggestions, and feedback are welcome as new features are added.
This project is a demo/learning project for Spring Boot development.
For questions or suggestions about this project, please open an issue in the repository.
Note: This README will be updated as new features and functionalities are added to the project.