# alexmanrique/spring-boot-application-example architecture

> Architecture diagram of the GitHub repository alexmanrique/spring-boot-application-example, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alexmanrique/spring-boot-application-example
- Repository: https://github.com/alexmanrique/spring-boot-application-example

## Overview

##### Project type and purpose
- This is a monolithic backend web service built with Spring Boot / Spring MVC.
- It exposes REST endpoints (versioned under /v1), secures them via Spring Security, and stores data in an in-memory H2 database.
- Key use case: manage Car and Driver entities via HTTP/Swagger calls.

##### File structure and README
- Top‐level:
– pom.xml / mvnw: Maven build and wrapper
– src/main/java/com/myapp: Java sources
– src/main/resources/application.properties: Spring Boot config + H2 init (data.sql)
- Logical packages under com.myapp:
– controller + mapper – REST & DTO conversion
– service – business logic
– dataaccessobject (DAO) – Spring Data repositories
– datatransferobject (DTO) – API models
– domainobject/value – core entities & enums
– security – WebSecurityConfigurer & initializer
– util – cross‐cutting LoggingInterceptor
- No separate “frontend” – Swagger UI is embedded in the application.

a. Client Layer
- REST clients (curl, Swagger UI, mobile/web clients)
- Swagger UI (served at /swagger-ui.html)
b. API / Web Layer
- Spring Security Filter Chain (MessageSecurityWebApplicationInitializer → SecurityConfiguration)
- LoggingInterceptor (captures incoming requests/logs)
- Controllers (CarController, DriverController, HomeController)
- DTO Mappers (CarMapper, DriverMapper)
c. Service Layer
- DefaultCarService, DefaultDriverService (implement CarService, DriverService)
- Business rules (e.g., throw CarAlreadyInUseException, ConstraintsViolationException)
d. Data Access Layer
- Spring Data Repositories (CarRepository, DriverRepository)
- Hibernate/JPA (implicit) → H2 in-memory database
e. Domain Model
- DomainObjects (CarDO, DriverDO)
- DomainValues (EngineType, GeoCoordinate, OnlineStatus)
f. Infrastructure / Build
- Maven build (pom.xml)
- Application launcher (MyappServer.java → SpringApplication.run)

##### Relationships and interactions
1. Client sends HTTP request → hits embedded web server (Tomcat via Spring Boot)
2. Spring Security intercepts → authenticates user
3. LoggingInterceptor logs request details
4. DispatcherServlet routes to appropriate Controller
5. Controller parses request → maps JSON to DTO → validates input
6. DTO passes to Service layer
7. Service applies business logic → interacts with DAO
8. DAO uses JPA/Hibernate to read/write H2 DB
9. Service returns DomainObject or DTO to Controller
10. Controller maps DomainObject → DTO → JSON response → client

##### Architectural patterns and design principles
- Layered (Presentation → Service → Data Access → Persistence)
- DTO pattern for decoupling API models from domain models
- Repository pattern (Spring Data) for persistence abstraction
- Dependency Injection (Spring) for wiring controllers, services, repositories
- Exception handling for domain‐specific errors
- Embedded Swagger for API discoverability

##### Detail
- Package as a distinct component.
- Classes for clarity (e.g., CarController, DefaultCarService).
- Cross‐cutting: logging interceptor applies to all controllers.
- Flow of DTO → DomainObject → DTO.
- Exception paths (e.g., service throws EntityNotFoundException → ControllerAdvice can handle).

## Diagram (Mermaid)

```mermaid
flowchart TD
    classDef external fill:#ffa500,stroke:#333,stroke-width:1px
    classDef appLayer fill:#add8e6,stroke:#333,stroke-width:1px
    classDef database fill:#90ee90,stroke:#333,stroke-width:1px
    classDef cloud fill:#f0e68c,stroke:#333,stroke-width:1px

    subgraph "Client Layer"
        Clients["Clients (curl, Postman)"]:::external
        SwaggerUI["Swagger UI (/swagger-ui.html)"]:::external
    end

    EmbeddedTomcat("Embedded Tomcat"):::cloud

    Clients -->|"HTTP Request"| EmbeddedTomcat
    SwaggerUI -->|"HTTP Request"| EmbeddedTomcat

    subgraph "Spring Boot Application"
        subgraph "Security Filter Chain"
            MSW["MessageSecurityWebApplicationInitializer"]:::appLayer
            SC["SecurityConfiguration"]:::appLayer
        end

        LI["LoggingInterceptor"]:::appLayer

        subgraph "Controllers + Mappers"
            CarCtrl["CarController (/v1/cars)"]:::appLayer
            DriverCtrl["DriverController (/v1/drivers)"]:::appLayer
            HomeCtrl["HomeController"]:::appLayer
            CarMapper["CarMapper"]:::appLayer
            DriverMapper["DriverMapper"]:::appLayer
        end

        subgraph "Service Layer"
            CarSvcI["CarService"]:::appLayer
            DriverSvcI["DriverService"]:::appLayer
            DefCarSvc["DefaultCarService"]:::appLayer
            DefDriverSvc["DefaultDriverService"]:::appLayer
        end

        subgraph "Data Access Layer"
            CarRepo["CarRepository"]:::appLayer
            DriverRepo["DriverRepository"]:::appLayer
        end

        subgraph "Infrastructure"
            Pom["pom.xml"]:::appLayer
            MyApp["MyappServer"]:::appLayer
        end
    end

    EmbeddedTomcat --> MSW
    MSW --> SC
    SC --> LI
    LI --> CarCtrl
    LI --> DriverCtrl
    LI --> HomeCtrl

    CarCtrl --> CarMapper
    DriverCtrl --> DriverMapper

    CarMapper --> CarSvcI
    DriverMapper --> DriverSvcI

    CarSvcI --> DefCarSvc
    DriverSvcI --> DefDriverSvc

    DefCarSvc --> CarRepo
    DefDriverSvc --> DriverRepo

    CarRepo -->|"JDBC"| H2
    DriverRepo -->|"JDBC"| H2

    H2["H2 In-Memory DB"]:::database

    MyApp --> EmbeddedTomcat

    click MSW "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/security/MessageSecurityWebApplicationInitializer.java"
    click SC "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/security/SecurityConfiguration.java"
    click LI "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/util/LoggingInterceptor.java"
    click CarCtrl "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/controller/CarController.java"
    click DriverCtrl "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/controller/DriverController.java"
    click HomeCtrl "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/controller/HomeController.java"
    click CarMapper "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/controller/mapper/CarMapper.java"
    click DriverMapper "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/controller/mapper/DriverMapper.java"
    click CarSvcI "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/service/car/CarService.java"
    click DriverSvcI "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/service/driver/DriverService.java"
    click DefCarSvc "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/service/car/DefaultCarService.java"
    click DefDriverSvc "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/service/driver/DefaultDriverService.java"
    click CarRepo "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/dataaccessobject/CarRepository.java"
    click DriverRepo "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/dataaccessobject/DriverRepository.java"
    click Pom "https://github.com/alexmanrique/spring-boot-application-example/blob/master/pom.xml"
    click MyApp "https://github.com/alexmanrique/spring-boot-application-example/blob/master/src/main/java/com/myapp/MyappServer.java"
```
