How alexmanrique/spring-boot-application-example works
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).