Skip to content

akhilkumar-dot/E-Commerce-Spring-Boot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ShopNex β€” Production-Style E-Commerce REST API

Java Spring Boot Maven JWT H2

A fully-featured, production-style e-commerce backend built with Spring Boot 3, Spring Security + JWT, and H2 in-memory database. Designed with clean architecture, role-based access control, and real-world business logic.


πŸ“¦ Tech Stack

Layer Technology
Framework Spring Boot 3.2.5
Security Spring Security + JJWT 0.11.5
Persistence Spring Data JPA + H2 (in-memory)
Validation Jakarta Bean Validation (@Valid)
DTO Mapping ModelMapper 3.2.0
Build Tool Maven
Boilerplate Lombok
Language Java 17

πŸ—‚οΈ Project Structure

src/main/java/com/shopnex/
β”œβ”€β”€ ShopNexApplication.java
β”œβ”€β”€ config/
β”‚   β”œβ”€β”€ AppConfig.java          # ModelMapper, BCrypt, AuthManager beans
β”‚   β”œβ”€β”€ SecurityConfig.java     # JWT filter chain, role-based URL rules
β”‚   └── DataSeeder.java         # Sample data loader (runs at startup)
β”œβ”€β”€ controller/
β”‚   β”œβ”€β”€ AuthController.java
β”‚   β”œβ”€β”€ CategoryController.java
β”‚   β”œβ”€β”€ ProductController.java
β”‚   β”œβ”€β”€ CartController.java
β”‚   β”œβ”€β”€ OrderController.java
β”‚   └── AdminController.java
β”œβ”€β”€ service/
β”‚   β”œβ”€β”€ AuthService.java
β”‚   β”œβ”€β”€ CategoryService.java
β”‚   β”œβ”€β”€ ProductService.java
β”‚   β”œβ”€β”€ CartService.java
β”‚   β”œβ”€β”€ OrderService.java
β”‚   └── serviceImpl/
β”‚       β”œβ”€β”€ AuthServiceImpl.java
β”‚       β”œβ”€β”€ CategoryServiceImpl.java
β”‚       β”œβ”€β”€ ProductServiceImpl.java
β”‚       β”œβ”€β”€ CartServiceImpl.java
β”‚       └── OrderServiceImpl.java
β”œβ”€β”€ repository/
β”‚   β”œβ”€β”€ UserRepository.java
β”‚   β”œβ”€β”€ CategoryRepository.java
β”‚   β”œβ”€β”€ ProductRepository.java
β”‚   β”œβ”€β”€ CartRepository.java
β”‚   β”œβ”€β”€ CartItemRepository.java
β”‚   └── OrderRepository.java
β”œβ”€β”€ model/entity/
β”‚   β”œβ”€β”€ User.java
β”‚   β”œβ”€β”€ Category.java
β”‚   β”œβ”€β”€ Product.java
β”‚   β”œβ”€β”€ Cart.java
β”‚   β”œβ”€β”€ CartItem.java
β”‚   β”œβ”€β”€ Order.java
β”‚   β”œβ”€β”€ OrderItem.java
β”‚   β”œβ”€β”€ Role.java               # Enum: CUSTOMER, ADMIN
β”‚   └── OrderStatus.java        # Enum: PENDING, CONFIRMED, SHIPPED, DELIVERED, CANCELLED
β”œβ”€β”€ dto/
β”‚   β”œβ”€β”€ request/
β”‚   β”‚   β”œβ”€β”€ RegisterRequest.java
β”‚   β”‚   β”œβ”€β”€ LoginRequest.java
β”‚   β”‚   β”œβ”€β”€ CategoryRequest.java
β”‚   β”‚   β”œβ”€β”€ ProductRequest.java
β”‚   β”‚   β”œβ”€β”€ CartItemRequest.java
β”‚   β”‚   └── OrderStatusUpdateRequest.java
β”‚   └── response/
β”‚       β”œβ”€β”€ AuthResponse.java
β”‚       β”œβ”€β”€ UserResponse.java
β”‚       β”œβ”€β”€ CategoryResponse.java
β”‚       β”œβ”€β”€ ProductResponse.java
β”‚       β”œβ”€β”€ CartItemResponse.java
β”‚       β”œβ”€β”€ CartResponse.java
β”‚       β”œβ”€β”€ OrderItemResponse.java
β”‚       └── OrderResponse.java
β”œβ”€β”€ security/
β”‚   β”œβ”€β”€ JwtTokenProvider.java
β”‚   β”œβ”€β”€ JwtAuthenticationFilter.java
β”‚   └── CustomUserDetailsService.java
└── exception/
    β”œβ”€β”€ ResourceNotFoundException.java
    β”œβ”€β”€ BadRequestException.java
    β”œβ”€β”€ UnauthorizedException.java
    β”œβ”€β”€ ApiError.java
    └── GlobalExceptionHandler.java

πŸš€ Setup & Run

Prerequisites

  • Java 17+
  • Maven 3.8+

Steps

# 1. Clone / navigate to project directory
cd E-Commerce

# 2. Build the project
mvn clean install

# 3. Run the application
mvn spring-boot:run

The API will start at: http://localhost:8080
H2 Console (dev only): http://localhost:8080/h2-console

  • JDBC URL: jdbc:h2:mem:shopnexdb
  • Username: sa | Password: (empty)

πŸ‘€ Pre-loaded Sample Data

Role Email Password
ADMIN admin@shopnex.com admin123
CUSTOMER john@shopnex.com john123

3 Categories: Electronics, Clothing, Books
10 Products: 4 Electronics, 3 Clothing, 3 Books


πŸ”‘ JWT Authentication β€” Sample Usage

Step 1 β€” Login and get token

curl -X POST http://localhost:8080/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"john@shopnex.com","password":"john123"}'

Response:

{
  "token": "eyJhbGciOiJIUzI1NiJ9...",
  "tokenType": "Bearer",
  "userId": 2,
  "name": "John Doe",
  "email": "john@shopnex.com",
  "role": "CUSTOMER"
}

Step 2 β€” Use token in requests

curl http://localhost:8080/api/cart \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9..."

πŸ“‹ Full API Endpoint Table

πŸ”“ Auth β€” Public

Method Endpoint Description Auth Required
POST /api/auth/register Register new customer ❌ No
POST /api/auth/login Login and receive JWT ❌ No

πŸ“‚ Categories

Method Endpoint Description Auth Required
GET /api/categories List all categories ❌ No
GET /api/categories/{id} Get category by ID ❌ No
POST /api/categories Create category βœ… ADMIN
PUT /api/categories/{id} Update category βœ… ADMIN
DELETE /api/categories/{id} Delete category βœ… ADMIN

πŸ›οΈ Products

Method Endpoint Description Auth Required
GET /api/products Paginated product list (+ filters) ❌ No
GET /api/products/{id} Get product by ID ❌ No
POST /api/products Create product βœ… ADMIN
PUT /api/products/{id} Update product βœ… ADMIN
DELETE /api/products/{id} Delete product βœ… ADMIN

Product Listing Query Parameters

Parameter Type Default Description
page int 0 Zero-based page index
size int 10 Number of items per page
sortBy string id Field to sort by (e.g., price)
sortDir string asc Sort direction: asc or desc
categoryId Long β€” Filter by category ID
keyword string β€” Search by product name

Example:

GET /api/products?page=0&size=5&sortBy=price&sortDir=asc&categoryId=1

πŸ›’ Cart β€” Requires Authentication (CUSTOMER)

Method Endpoint Description Auth Required
GET /api/cart View cart with grand total βœ… CUSTOMER
POST /api/cart/items Add item to cart βœ… CUSTOMER
PUT /api/cart/items/{itemId} Update item quantity βœ… CUSTOMER
DELETE /api/cart/items/{itemId} Remove item from cart βœ… CUSTOMER

Add to cart body:

{
  "productId": 3,
  "quantity": 2
}

πŸ“¦ Orders β€” Requires Authentication (CUSTOMER)

Method Endpoint Description Auth Required
POST /api/orders Place order from cart βœ… CUSTOMER
GET /api/orders View my order history βœ… CUSTOMER
GET /api/orders/{id} View specific order (own only) βœ… CUSTOMER

πŸ”§ Admin Panel β€” Requires ADMIN Role

Method Endpoint Description
POST /api/admin/products Create product
PUT /api/admin/products/{id} Update product
DELETE /api/admin/products/{id} Delete product
GET /api/admin/orders View all orders (paginated)
GET /api/admin/orders/{id} View any order by ID
PATCH /api/admin/orders/{id}/status Update order status

Update order status body:

{
  "status": "CONFIRMED"
}

Valid statuses: PENDING β†’ CONFIRMED β†’ SHIPPED β†’ DELIVERED | CANCELLED


⚠️ Error Response Format

All errors return a consistent JSON structure:

{
  "status": 400,
  "error": "VALIDATION_FAILED",
  "message": "Input validation failed. Please check field errors.",
  "timestamp": "2024-01-15T10:30:00",
  "fieldErrors": {
    "email": "Email must be a valid email address",
    "password": "Password must be at least 6 characters"
  }
}

πŸ” Role-Based Access Summary

Feature CUSTOMER ADMIN
Register / Login βœ… βœ…
Browse products/categories βœ… βœ…
Manage cart βœ… ❌
Place/view own orders βœ… ❌
Manage products ❌ βœ…
Manage categories ❌ βœ…
View all orders ❌ βœ…
Update order status ❌ βœ…

πŸ—ƒοΈ Database Schema Overview

users           β†’ id, name, email, password, role
categories      β†’ id, name, description
products        β†’ id, name, description, price, stock_quantity, image_url, category_id
carts           β†’ id, user_id
cart_items      β†’ id, cart_id, product_id, quantity
orders          β†’ id, user_id, total_amount, status, created_at, updated_at
order_items     β†’ id, order_id, product_id, quantity, price_at_purchase

πŸ“ Notes

  • JWT tokens expire in 24 hours (configurable via shopnex.jwt.expiration-ms)
  • H2 database is reset on every restart β€” data is re-seeded automatically
  • Passwords are hashed with BCrypt
  • Price history is snapshotted at order time β€” future product price changes don't affect past orders
  • Cart is automatically cleared after a successful order placement

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages