How spring-petclinic/spring-petclinic-rest works
Project type and purpose
• This is a backend‐only REST API microservice for the Spring PetClinic domain.
• It exposes CRUD endpoints for Owners, Pets, Vets, Specialties, PetTypes, Visits, and Users.
• It has no built‐in UI; an external Angular client or other HTTP clients consume the API.
File structure
• Top‐level:
• pom.xml, Maven wrapper, GitHub Actions workflows, Docker compose, Postman/Newman scripts, JMeter plan.
• src/main/java/org/springframework/samples/petclinic: Java sources.
• src/main/resources: Spring Boot properties, multi-profile database scripts, OpenAPI spec, logging, i18n.
• src/test: unit/integration tests, performance (JMeter), API tests (Postman/Newman).
• Under Java sources:
• PetClinicApplication.java (Spring Boot entry point).
• config: Swagger/OpenAPI configuration.
• rest/controller & rest/advice: HTTP controllers + exception handling.
• service: business logic (ClinicService, UserService).
• repository: three implementations layers—JDBC, JPA, Spring Data JPA.
• mapper: DTO ↔ Entity mapping (MapStruct).
• model: domain entities (Owner, Pet, Vet, Visit, etc.).
• security: optional Basic authentication config and role definitions.
• util: cross-cutting concerns (monitoring aspect, entity utilities).
README for insights
• Supports multiple databases via Spring profiles (H2, HSQLDB, MySQL, PostgreSQL).
• Exposes Swagger UI and OpenAPI (openapi.yml).
• Uses Google Jib for Docker image builds.
• CI pipelines for Maven build, Docker build, and Newman tests.
• API-first approach: code generation for DTOs and API interfaces during Maven build.
a. Main components
• External Clients: Swagger UI, Postman/Newman, Angular front-end (optional), curl/HTTP clients
• API Gateway Layer: Spring MVC REST Controllers
• Service Layer: ClinicService, UserService
• Data Access Layer:
• Repository interfaces
• Implementations: JDBC, JPA, Spring Data JPA
• Domain Model: Entities and DTOs (MapStruct mappers)
• Persistence: Relational Database(s) (H2/HSQLDB/MySQL/Postgres)
• Infrastructure Components:
• Spring Boot App (PetClinicApplication)
• Swagger/OpenAPI
• Actuator (health endpoint)
• Security Module (BasicAuth filter when enabled)
• CI/CD and Testing: GitHub Actions, Docker/Jib, JMeter, Newman/Postman
b. Relationships and interactions
• Clients send HTTP requests → Controllers → Service Layer → Repository Layer → Database
• Controllers use DTOs; mappers convert between DTOs and Entities
• Service Layer may enforce transactions, business rules
• Repository Layer selects implementation based on active Spring profile
• OpenAPI spec drives code generation → generated DTOs and API interfaces appear in target/generated-sources
• Actuator and Swagger are auto‐configured by Spring Boot
c. Architectural patterns and principles
• Layered architecture: Controller → Service → Repository → Database
• API‐first: OpenAPI specification, code generation
• Dependency Injection via Spring Boot
• Profile-based bean selection for multiple database implementations
• Cross‐cutting aspects: security, logging/monitoring (AOP)
d. Key technologies & frameworks
• Spring Boot, Spring MVC, Spring Data JPA, JDBC, MapStruct, OpenAPI Generator, Google Jib
• Databases: H2, HSQLDB, MySQL, PostgreSQL
• Testing: JUnit, Spring Test, JMeter, Postman/Newman
• CI/CD: GitHub Actions, Docker Compose