A comprehensive Spring Boot REST API for managing a pet shop business, including pets, customers, employees, suppliers, transactions, and various services.
- Pet Management: CRUD operations for pets with categories, breeds, and pricing
- Customer Management: Customer registration and profile management options
- Employee Management: Staff information added and role management
- Supplier Management: Supplier contact and relationship tracking
- Transaction Tracking: Sales and payment management processing
- Service Management: Grooming services and vaccination records
- Inventory Management: Pet food and supplies tracking management
- Framework: Spring Boot 3.2.0
- Database: MySQL (primary), H2 (testing)
- ORM: Spring Data JPA with Hibernate and spring boot
- Security: Spring Security and authentication
- Validation: Bean Validation (Jakarta Validation)
- Mapping: MapStruct
- Build Tool: Maven
- Java Version: 17+
- Use of java 8 features
src/main/java/com/petshop/
├── controller/ # REST controllers
├── service/ # Business logic services
├── repository/ # Data access layer
├── entity/ # JPA entities
├── dto/ # Data transfer objects
├── mapper/ # MapStruct mappers
├── exception/ # Custom exceptions and handlers
├── config/ # Configuration classes
└── PetShopApplication.java
The application implements the following main entities:
- Address: Physical addresses for customers, employees, suppliers
- Pet: Pet information with categories and pricing
- Customer: Customer profiles and contact information
- Employee: Staff information and roles
- Supplier: Vendor information and contacts
- Transaction: Sales and payment records
- PetCategory: Pet classification system
- PetFood: Inventory management for pet food
- GroomingService: Available grooming services
- Vaccination: Vaccination types and records
- Java 17 or higher
- Maven 3.6+
- MySQL 8.0+ (for production)
- IDE (IntelliJ IDEA, Eclipse, STS or VS Code)
git clone <your-repository-url>
cd petshop-backend- Install MySQL and create a database:
CREATE DATABASE petshop_db;- Update
application.ymlwith your MySQL credentials:
spring:
datasource:
url: jdbc:mysql://localhost:3306/petshop_db?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC
username: your_username
password: your_passwordRun with the dev profile to use H2 in-memory database:
mvn spring-boot:run -Dspring-boot.run.profiles=dev# Install dependencies
mvn clean install
# Run the application
mvn spring-boot:run- Import the project as a Maven project
- Run the
PetShopApplication.javamain class
The application will start on http://localhost:8080
API base URL: http://localhost:8080/api/v1
GET /api/v1/pets- Get all petsGET /api/v1/pets/{id}- Get pet by IDPOST /api/v1/pets- Create new petPUT /api/v1/pets/{id}- Update petDELETE /api/v1/pets/{id}- Delete petGET /api/v1/pets/category/{categoryId}- Get pets by categoryGET /api/v1/pets/breed/{breed}- Get pets by breedGET /api/v1/pets/price-range?minPrice={min}&maxPrice={max}- Get pets by price rangeGET /api/v1/pets/search?name={name}- Search pets by name
GET /api/v1/customers- Get all customersGET /api/v1/customers/{id}- Get customer by IDPOST /api/v1/customers- Create new customerPUT /api/v1/customers/{id}- Update customerDELETE /api/v1/customers/{id}- Delete customerGET /api/v1/customers/search?name={name}- Search customers by nameGET /api/v1/customers/email/{email}- Get customer by email
GET /api/v1/transactions- Get all transactionsGET /api/v1/transactions/{id}- Get transaction by IDGET /api/v1/transactions/customer/{customerId}- Get transactions by customerGET /api/v1/transactions/pet/{petId}- Get transactions by pet
GET /api/v1/categories- Get all categoriesGET /api/v1/categories/{id}- Get category by IDPOST /api/v1/categories- Create new categoryPUT /api/v1/categories/{id}- Update categoryDELETE /api/v1/categories/{id}- Delete category
curl -X POST http://localhost:8080/api/v1/pets \
-H "Content-Type: application/json" \
-d '{
"name": "Buddy",
"breed": "Golden Retriever",
"age": 2,
"price": 1200.00,
"description": "Friendly and energetic dog",
"imageUrl": "https://example.com/buddy.jpg",
"categoryId": 1
}'curl -X POST http://localhost:8080/api/v1/customers \
-H "Content-Type: application/json" \
-d '{
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@email.com",
"phoneNumber": "555-0101",
"addressId": 1
}'curl http://localhost:8080/api/v1/petsWhen running with the dev profile, you can access the H2 database console at:
http://localhost:8080/h2-console
- JDBC URL:
jdbc:h2:mem:testdb - Username:
admin - Password: (leave empty)
The application loads sample data automatically when using H2 database, including:
- 3 addresses
- 5 pet categories (Dogs, Cats, Birds, Fish, Reptiles)
- 3 customers
- 3 employees
- 2 suppliers
- 4 pets
- Pet food items
- Grooming services
- Vaccination types
Key configuration options in application.yml:
server:
port: 8080
servlet:
context-path: /api/v1
spring:
jpa:
hibernate:
ddl-auto: update # Creates/updates tables automatically
show-sql: true # Shows SQL queries in logs
jackson:
serialization:
write-dates-as-timestamps: false- default: Uses MySQL database
- dev: Uses H2 in-memory database for development
The application includes comprehensive error handling:
- 404 Not Found: When resources don't exist
- 400 Bad Request: For validation errors
- 500 Internal Server Error: For unexpected errors
All errors return structured JSON responses:
{
"status": 404,
"error": "Resource Not Found",
"message": "Pet not found with id: 999",
"timestamp": "2024-01-15T10:30:00"
}Currently configured for development with:
- CORS enabled for all origins
- CSRF disabled
- All endpoints publicly accessible
For production, implement:
- JWT authentication
- Role-based access control
- API rate limiting
- Input sanity and Dependency
-
Adding New Entities: Follow the established pattern:
- Create entity class in
entitypackage - Add repository interface in
repositorypackage - Create DTO in
dtopackage - Add mapper interface in
mapperpackage - Implement service in
servicepackage - Create controller in
controllerpackage
- Create entity class in
-
Database Changes:
- Modify entity classes
- Set
ddl-auto: updatefor automatic schema updates - Or use Flyway/Liquibase for production migrations
-
Testing:
- Use H2 for integration tests
- MockMvc for controller testing
- TestContainers for database integration tests
-
Port 8080 already in use:
# Change port in application.yml or run with different port mvn spring-boot:run -Dserver.port=8081 -
Database connection failed:
- Verify MySQL is running
- Check database credentials in
application.yml - Ensure database exists
-
Build failures:
# Clean and rebuild mvn clean install -U this will the final command that we have to use
- JWT Authentication & Authorization
- File upload for pet images
- Email notifications
- Inventory management
- Reporting and analytics
- Payment gateway integration
- Appointment scheduling system
- Real-time notifications