How hbbugrakaya/spring-rest-gym-crm works
Purpose and Runtime
Gym CRM is a Java 21 Spring MVC REST API for managing users, trainee/trainer profiles and assignments, training sessions, and a fixed training-type catalog. It is a classic WAR, not Spring Boot: a Servlet 6 container starts it through src/main/java/com/example/gymcrm/web/config/WebAppInitializer.java. Build/deployment is Maven/WAR-driven via pom.xml.
Web Entry and HTTP Boundary
WebAppInitializer registers the DispatcherServlet and cross-cutting filter; src/main/java/com/example/gymcrm/web/config/WebConfig.java configures the MVC layer. Controllers are the public API entry points, including profile/training operations and authentication:
• src/main/java/com/example/gymcrm/web/controller/TraineeController.java
• src/main/java/com/example/gymcrm/web/controller/TrainerController.java
• src/main/java/com/example/gymcrm/web/controller/TrainingController.java
Requests and responses are isolated from core models through DTOs and src/main/java/com/example/gymcrm/web/mapper/GymWebMapper.java.
Authentication, Request Context, and Errors
Registration endpoints are public; other business endpoints use HTTP Basic credentials resolved by src/main/java/com/example/gymcrm/web/security/RequestCredentialsResolver.java and validated through the authentication service. src/main/java/com/example/gymcrm/web/filter/TransactionIdFilter.java accepts or generates a canonical transaction UUID, places it in MDC, and returns it in response headers. src/main/java/com/example/gymcrm/web/error/RestExceptionHandler.java converts validation, authentication, missing-entity, and profile-state failures into a consistent API error response.
Application Coordination Layer
Controllers invoke service interfaces, while src/main/java/com/example/gymcrm/facade/GymFacade.java remains the compatibility/coordination boundary from the preceding module. Service commands encode writes and criteria encode filtered training queries:
• src/main/java/com/example/gymcrm/service/command/CreateTraineeCommand.java
• src/main/java/com/example/gymcrm/service/command/AddTrainingCommand.java
• src/main/java/com/example/gymcrm/service/criteria/TraineeTrainingCriteria.java
Core workflows include registration with generated credentials, profile replacement, trainer assignment replacement, status/password changes, and training creation.
Domain and Business Services
The domain models users, role-specific profiles, sessions, and training types:
• src/main/java/com/example/gymcrm/domain/User.java
• src/main/java/com/example/gymcrm/domain/Trainee.java
• src/main/java/com/example/gymcrm/domain/Trainer.java
Implementation services enforce business rules and orchestrate persistence, including specialization-derived training creation and active/unassigned trainer lookup:
• src/main/java/com/example/gymcrm/service/impl/TraineeServiceImpl.java
• src/main/java/com/example/gymcrm/service/impl/TrainerServiceImpl.java
• src/main/java/com/example/gymcrm/service/impl/TrainingServiceImpl.java
Generated usernames and one-time registration passwords originate from src/main/java/com/example/gymcrm/generator/UniqueUsernameGenerator.java and src/main/java/com/example/gymcrm/generator/SecurePasswordGenerator.java.
Persistence and Initialization
Hibernate-backed repositories are the persistence boundary:
• src/main/java/com/example/gymcrm/repository/UserRepository.java
• src/main/java/com/example/gymcrm/repository/TrainingRepository.java
• src/main/java/com/example/gymcrm/repository/TrainingTypeRepository.java
src/main/java/com/example/gymcrm/config/PersistenceConfig.java supplies ORM, datasource, and transaction configuration. Default local infrastructure is in-memory H2; JDBC system properties permit an external database such as PostgreSQL. src/main/java/com/example/gymcrm/config/TrainingTypeInitializer.java initializes the immutable training-type catalog.
Primary Flow and Boundaries
HTTP request → transaction filter/security credential resolution → controller DTO validation/mapping → facade/service command or criteria → Hibernate repository/domain graph → DTO response. Transactional persistence and ORM concerns stop at repositories/configuration; HTTP semantics, Basic headers, validation payloads, and error serialization remain in web. Services raise typed exceptions from src/main/java/com/example/gymcrm/exception, which the global handler maps back to HTTP status codes.
Main entry points: TraineeServiceImpl.java Business services · TraineeController.java REST controllers · WebAppInitializer.java Web app initializer · UserRepository.java Hibernate repositories
Components 18
Runtime
- Maven WAR build · Packages the application for a Servlet 6 container.
pom.xml - Web app initializer · Registers DispatcherServlet and cross-cutting filter.
src/main/java/com/example/gymcrm/web/config/WebAppInitializer.java
Web boundary
- MVC configuration
src/main/java/com/example/gymcrm/web/config/WebConfig.java - Transaction ID filter · Propagates a canonical transaction UUID through MDC and response headers.
src/main/java/com/example/gymcrm/web/filter/TransactionIdFilter.java - Basic credentials resolver
src/main/java/com/example/gymcrm/web/security/RequestCredentialsResolver.java - REST controllers · Profile, assignment, training, and registration endpoints.
src/main/java/com/example/gymcrm/web/controller/TraineeController.java - Web DTO mapper · Isolates HTTP DTOs from core models.
src/main/java/com/example/gymcrm/web/mapper/GymWebMapper.java - REST exception handler · Maps typed business failures to consistent API errors.
src/main/java/com/example/gymcrm/web/error/RestExceptionHandler.java
Application
- Gym facade · Coordination boundary retained from the preceding module.
src/main/java/com/example/gymcrm/facade/GymFacade.java - Write commands · Encodes registration and training write requests.
src/main/java/com/example/gymcrm/service/command/CreateTraineeCommand.java - Training criteria · Encodes filtered trainee training queries.
src/main/java/com/example/gymcrm/service/criteria/TraineeTrainingCriteria.java - Business services · Enforces profile, assignment, status, password, and training rules.
src/main/java/com/example/gymcrm/service/impl/TraineeServiceImpl.java - Username generator
src/main/java/com/example/gymcrm/generator/UniqueUsernameGenerator.java - Password generator · Creates one-time registration passwords.
src/main/java/com/example/gymcrm/generator/SecurePasswordGenerator.java
Domain & persistence
- User and profile domain · Models users and trainee/trainer role profiles.
src/main/java/com/example/gymcrm/domain/User.java - Hibernate repositories · Persists users, sessions, and training types.
src/main/java/com/example/gymcrm/repository/UserRepository.java - ORM and transactions · Configures Hibernate, datasource, and transactions for H2 or external JDBC databases.
src/main/java/com/example/gymcrm/config/PersistenceConfig.java - Training-type catalog · Fixed catalog initialized on application setup.
src/main/java/com/example/gymcrm/config/TrainingTypeInitializer.java
Connections 22
- Maven WAR build → Web app initializer: packages
- Web app initializer → MVC configuration: starts MVC
- Web app initializer → Transaction ID filter: registers
- Transaction ID filter → REST controllers: request with transaction context
- MVC configuration → REST controllers: configures
- REST controllers → Basic credentials resolver: resolves Basic auth
- REST controllers → Web DTO mapper: maps DTOs
- REST controllers → Gym facade: coordinates through
- REST controllers → Business services: invokes services
- Web DTO mapper → User and profile domain: maps core models
- Gym facade → Business services: delegates workflows
- Write commands → Business services: write input
- Training criteria → Business services: query input
- Basic credentials resolver → Business services: validates user
- Business services → Username generator: generates registration name
- Business services → Password generator: generates registration password
- Business services → User and profile domain: applies business rules
- Business services → Hibernate repositories: persists and queries
- Hibernate repositories → ORM and transactions: uses ORM transactions
- Training-type catalog → Hibernate repositories: initializes catalog
- Business services → REST exception handler: raises typed exceptions
- REST exception handler → REST controllers: serializes API errors