Skip to content

Barcodehub/hexagonal-archi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ—οΈ Hexagonal Architecture with Spring Boot

Personal exploration project to implement Hexagonal Architecture (Clean Architecture) alongside modern automation tools in the Spring Boot ecosystem.

πŸ“‹ Table of Contents

πŸ“– Description

This project implements a product management system using Hexagonal Architecture, with the goal of exploring clean architecture patterns and code automation tools. The system exposes a complete REST API for CRUD operations on products.

Project Goals

  • Implement Hexagonal Architecture with clear separation of concerns
  • Automate API code generation using OpenAPI Generator
  • Explore modern tools in the Spring Boot ecosystem
  • Apply development and testing best practices

πŸ›οΈ Architecture

The project follows Hexagonal Architecture principles by organizing code into the following layers:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚            🌐 API Layer                 β”‚
β”‚        (Controllers, DTOs)              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚         🧠 Application Layer            β”‚
β”‚       (Services, Use Cases)            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚          πŸ’Ž Domain Layer                β”‚
β”‚    (Entities, Business Rules)          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚       πŸ”Œ Infrastructure Layer          β”‚
β”‚    (Repositories, Database, JPA)       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Ports and Adapters

  • Port: ProductRepository (domain interface)
  • Adapter: ProductRepositoryAdapter (JPA implementation)

πŸ› οΈ Tech Stack

Technology Version Purpose
Java 17 Base language
Spring Boot 3.x Main framework
Spring Data JPA - Data persistence
PostgreSQL - Database
OpenAPI Generator - Automatic API generation
MapStruct - Layer mapping
Lombok - Boilerplate reduction
Maven - Dependency management
Hibernate Validator - Data validation

πŸ“ Project Structure

barcodehub/
β”œβ”€β”€ domain/                     # πŸ’Ž Domain Layer
β”‚   β”œβ”€β”€ src/main/java/
β”‚   β”‚   β”œβ”€β”€ model/
β”‚   β”‚   β”‚   └── Product.java
β”‚   β”‚   └── exception/
β”‚   β”‚       β”œβ”€β”€ NotFoundException.java
β”‚   β”‚       └── BusinessErrorCodes.java
β”‚   └── pom.xml
β”œβ”€β”€ app-service/                # 🧠 Application Layer
β”‚   β”œβ”€β”€ src/main/java/
β”‚   β”‚   β”œβ”€β”€ service/
β”‚   β”‚   β”œβ”€β”€ port/
β”‚   β”‚   β”‚   └── ProductRepository.java
β”‚   β”‚   └── mapper/
β”‚   β”‚       └── ProductMapper.java
β”‚   └── pom.xml
β”œβ”€β”€ infrastructure/             # πŸ”Œ Infrastructure Layer
β”‚   β”œβ”€β”€ src/main/java/
β”‚   β”‚   β”œβ”€β”€ adapter/
β”‚   β”‚   β”‚   └── ProductRepositoryAdapter.java
β”‚   β”‚   β”œβ”€β”€ entity/
β”‚   β”‚   β”‚   └── ProductEntity.java
β”‚   β”‚   β”œβ”€β”€ repository/
β”‚   β”‚   β”‚   └── JpaProductRepository.java
β”‚   β”‚   β”œβ”€β”€ controller/
β”‚   β”‚   β”‚   └── ProductController.java
β”‚   β”‚   └── mapper/
β”‚   β”‚       └── ProductEntityMapper.java
β”‚   β”œβ”€β”€ src/main/resources/
β”‚   β”‚   β”œβ”€β”€ openapi.yaml
β”‚   β”‚   └── application.properties
β”‚   └── pom.xml
└── pom.xml                     # Parent POM

πŸš€ Installation and Execution

Prerequisites

  • Java 17+
  • Maven 3.8+
  • PostgreSQL 12+

Installation Steps

  1. Clone the repository

    git clone <repository-url>
    cd barcodehub
  2. Setup database

    CREATE DATABASE productdb;
  3. Configure properties (optional)

    # src/main/resources/application.properties
    spring.datasource.url=jdbc:postgresql://localhost:5432/productdb
    spring.datasource.username=your_user_postgre
    spring.datasource.password=your_password_postgre
  4. Compile the project

    mvn clean compile
  5. Run the application

    mvn spring-boot:run -pl infrastructure

The application will be available at http://localhost:8080

🌐 API Endpoints

Method Endpoint Description
GET /products Get all products
POST /products Create a new product
GET /products/{id} Get product by ID
PUT /products/{id} Update existing product
DELETE /products/{id} Delete product

Example Payload

{
  "name": "Dell XPS Laptop",
  "price": 1299.99
}

✨ Technical Features

πŸ”„ Automatic Code Generation

  • OpenAPI Generator automatically generates:
    • Controller interfaces (ProductApi)
    • API DTOs (ProductDto, DataProductListDto)
    • Validation annotations
    • API documentation

πŸ—ΊοΈ Automatic Layer Mapping

  • MapStruct handles mapping between:
    • Product (Domain) ↔ ProductEntity (JPA)
    • Product (Domain) ↔ ProductDto (API)

🧩 Multi-module Maven Configuration

  • OpenAPI Generator Plugin: Compile-time generation
  • Build Helper Plugin: Generated code integration
  • Annotation Processing: Lombok + MapStruct

πŸ’‘ Key Learnings

βœ… Hexagonal Architecture

  • Clear separation of responsibilities facilitates testing and maintenance
  • Ports and adapters allow changing implementations without affecting domain
  • Dependency inversion makes code more testable and flexible

βœ… Code Automation

  • OpenAPI Generator significantly reduces boilerplate code
  • YAML specifications as single source of truth for API
  • Build-time generation keeps code and documentation synchronized

βœ… Spring Ecosystem Tools

  • Spring Data JPA simplifies persistence operations
  • MapStruct is incredibly efficient for object mapping
  • Maven multi-module better organizes complex projects

βœ… Best Practices

  • Domain validation keeps business rules centralized
  • Custom exceptions improve error handling
  • Layer-specific DTOs avoid coupling

🀝 Contributing

This is a personal learning project, but if you find improvements or have suggestions, feel free to:

  1. Fork the project
  2. Create a feature branch (git checkout -b feature/improvement)
  3. Commit your changes (git commit -am 'Add new improvement')
  4. Push to the branch (git push origin feature/improvement)
  5. Create a Pull Request

⭐ If you found this project useful, don't forget to give it a star

πŸ“¬ Have questions? Feel free to open an issue or contact me directly.


Developed with ❀️ to explore modern architectures and development best practices

About

arquitectura hexagonal template one (crud de productos spring+postgre)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages