ShopNex β Production-Style E-Commerce REST API
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.
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
# 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
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
Method
Endpoint
Description
Auth Required
POST
/api/auth/register
Register new customer
β No
POST
/api/auth/login
Login and receive JWT
β No
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
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
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