Skip to content

mateusribeirocampos/spring-boot-java2025

Repository files navigation

REST API with Spring Boot and Java

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.

πŸš€ Features

  • 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

πŸ› οΈ Technologies Used

  • Java 21
  • Spring Boot 3.4.0
  • Spring Data JPA
  • MySQL
  • DozerMapper 7.0.0
  • Maven
  • JUnit 5 (for testing)

πŸ“ Project Structure

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

πŸš€ Getting Started

Prerequisites

  • Java 21 or higher
  • Maven 3.6+
  • MySQL 8.0+

Environment Variables

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

Installation

  1. Clone the repository

    git clone <repository-url>
    cd spring-boot-and-java
  2. Build the project

    mvn clean install
  3. Run the application

    mvn spring-boot:run

The application will start on http://localhost:8080

πŸ“š API Documentation

Person Management API

Base URL: /person

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

API Versioning

The API supports multiple versions:

  • v1: Standard PersonDTO with basic fields
  • v2: Enhanced PersonDTOV2 with additional fields and features

Request/Response Examples

Create Person (v1)

POST /person
Content-Type: application/json

{
  "firstName": "John",
  "lastName": "Doe",
  "address": "123 Main St",
  "gender": "Male"
}

Create Person (v2)

POST /person/v2
Content-Type: application/json

{
  "firstName": "Jane",
  "lastName": "Smith",
  "address": "456 Oak Ave",
  "gender": "Female"
  // Additional v2-specific fields
}

πŸ—„οΈ Database Schema

Person Table

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
);

πŸ”§ Configuration

Application Configuration (application.yml)

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

πŸ§ͺ Testing

Running Tests

# Run all tests
mvn test

# Run specific test class
mvn test -Dtest=ObjectMapperTests

Test Structure

  • 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

πŸ“Š Architecture

Layered Architecture

  1. Controller Layer: Handles HTTP requests and responses
  2. Service Layer: Contains business logic
  3. Repository Layer: Data access and persistence
  4. Model Layer: JPA entities representing database tables

Design Patterns

  • 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

πŸ”„ Object Mapping

The application uses two mapping approaches:

  1. DozerMapper: Generic object mapping utility
  2. Custom Mappers: Specialized mappers for complex object transformations (e.g., PersonMapper)

πŸ“ Logging

Configurable logging levels:

  • DEBUG: Detailed application flow (br.com.campos package)
  • INFO: General application information
  • WARN/ERROR: Issues and exceptions

🚧 Development Status

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

🀝 Contributing

This project is still under development. Contributions, suggestions, and feedback are welcome as new features are added.

πŸ“„ License

This project is a demo/learning project for Spring Boot development.

πŸ“ž Contact

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.

About

Spring Boot framework Swagger Docker Kubernetes API Rest JWT JUnit 5 Mockito ReactJs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published