A comprehensive RESTful API for an e-commerce application built with Spring Boot. This backend solution provides complete functionality for user authentication, product catalog management, order processing, and cart operations.
- ๐ Authentication & Authorization: JWT-based security with role-based access control (USER/ADMIN)
- ๐ฆ Product Management: Full CRUD operations for products with category support
- ๐ Shopping Cart: Add, update, remove items with quantity management
- ๐ Order Management: Complete order lifecycle management
- ๐ค User Management: User registration, profile management, and address handling
- ๐ฏ Category Management: Organize products with hierarchical categories
- ๐ Search & Pagination: Advanced product search with pagination support
- ๐ฑ REST API: Clean, RESTful endpoints with proper HTTP status codes
| Technology | Purpose |
|---|---|
| Java 17 | Programming language |
| Spring Boot 3.3.3 | Application framework |
| Spring Security | Authentication & authorization |
| Spring Data JPA | Data persistence layer |
| Hibernate | ORM for database operations |
| JWT (JSON Web Tokens) | Stateless authentication |
| MySQL/H2 | Database (configurable) |
| Maven | Build automation & dependency management |
| Lombok | Reduce boilerplate code |
| ModelMapper | Object mapping |
Before running this application, make sure you have:
- Java 17 or higher installed
- Maven 3.8+ for build management
- MySQL database (or use embedded H2 for development)
- Git for version control
git clone https://github.com/ericndungutse/springboot-ecommerce.git
cd springboot-ecommerceCreate an application.properties file in src/main/resources/:
# Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/ecommerce_db
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JPA Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
# JWT Configuration
app.jwtSecret=mySecretKey
app.jwtExpirationMs=86400000
# Server Configuration
server.port=8080# H2 Database Configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.h2.console.enabled=true
# JPA Configuration
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# JWT Configuration
app.jwtSecret=mySecretKey
app.jwtExpirationMs=86400000# Build the project
mvn clean compile
# Run the application
mvn spring-boot:runThe application will start on http://localhost:8080
Check if the application is running:
curl http://localhost:8080/api/public/categoriessrc/
โโโ main/
โ โโโ java/
โ โโโ com/ecommerce/emarket/
โ โโโ config/ # Configuration classes
โ โโโ controller/ # REST controllers
โ โโโ exceptions/ # Custom exception handling
โ โโโ model/ # JPA entity classes
โ โโโ payload/ # DTOs and response classes
โ โโโ repositories/ # Data access layer
โ โโโ security/ # Security configuration
โ โโโ service/ # Business logic layer
โ โโโ utils/ # Utility classes
โโโ test/ # Test classes
This API uses JWT (JSON Web Tokens) for authentication. To access protected endpoints:
- Register a new user or login with existing credentials
- Use the returned JWT token in the
Authorizationheader:Bearer <token>
On first startup, you can create an admin user through the registration endpoint with admin privileges.
http://localhost:8080/api
| Method | Endpoint | Description | Access |
|---|---|---|---|
| POST | /auth/signin |
User login | Public |
| POST | /auth/signup |
User registration | Public |
| Method | Endpoint | Description | Access |
|---|---|---|---|
| GET | /public/categories |
Get all categories | Public |
| POST | /admin/categories |
Create category | Admin |
| PUT | /admin/categories/{id} |
Update category | Admin |
| DELETE | /admin/categories/{id} |
Delete category | Admin |
| Method | Endpoint | Description | Access |
|---|---|---|---|
| GET | /public/products |
Get all products | Public |
| GET | /public/categories/{categoryId}/products |
Get products by category | Public |
| POST | /admin/categories/{categoryId}/products |
Add product | Admin |
| PUT | /admin/products/{id} |
Update product | Admin |
| DELETE | /admin/products/{id} |
Delete product | Admin |
| Method | Endpoint | Description | Access |
|---|---|---|---|
| GET | /users/carts |
Get user cart | User |
| POST | /carts/products/{productId}/quantity/{quantity} |
Add to cart | User |
| PUT | /carts/products/{productId}/quantity/{quantity} |
Update cart item | User |
| DELETE | /carts |
Clear cart | User |
| Method | Endpoint | Description | Access |
|---|---|---|---|
| GET | /users/orders |
Get user orders | User |
| POST | /users/orders |
Create order | User |
| GET | /admin/orders |
Get all orders | Admin |
| Method | Endpoint | Description | Access |
|---|---|---|---|
| GET | /users/addresses |
Get user addresses | User |
| POST | /users/addresses |
Add address | User |
| PUT | /users/addresses/{id} |
Update address | User |
| DELETE | /users/addresses/{id} |
Delete address | User |
Default pagination settings can be modified in AppConstants.java:
- Page Size: 2 items per page
- Sort Order: Ascending
- Sort Field: ID field
The application supports file uploads for product images. Configure the upload directory in your application.properties.
Run tests with Maven:
# Run all tests
mvn test
# Run specific test class
mvn test -Dtest=EmarketApplicationTests- Build the JAR file:
mvn clean package- Run the JAR:
java -jar target/emarket-0.0.1-SNAPSHOT.jarcurl -X POST http://localhost:8080/api/auth/signup \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"email": "john@example.com",
"password": "password123",
"role": ["user"]
}'curl -X POST http://localhost:8080/api/auth/signin \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"password": "password123"
}'curl -X GET "http://localhost:8080/api/public/products?pageNumber=1&pageSize=10"curl -X POST http://localhost:8080/api/carts/products/1/quantity/2 \
-H "Authorization: Bearer YOUR_JWT_TOKEN"- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any problems or have questions:
- Check the Issues page
- Create a new issue with detailed description
- Contact the maintainers
- Email notification system
- Payment gateway integration (Stripe, PayPal)
- Product reviews and ratings
- Inventory management
- Advanced search filters
- Docker containerization
- API rate limiting
- Caching with Redis