A Spring Boot and MongoDB backend that models a small e-commerce workflow across products, carts, orders, and payments. It was created as coursework while learning Java backend development and is presented as a supporting learning project rather than a production-ready system.
- Create and list products
- Add products to a user's cart, view the cart, and clear it
- Convert cart items into an order and calculate the total from current product prices
- Reduce product stock during order creation
- Create a pending payment and process a simulated payment webhook
- Update an order to
PAIDorFAILEDfrom the webhook result - Validate cart, order, payment, and webhook request DTOs
- Java 17
- Spring Boot 3.5
- Spring Web and Bean Validation
- Spring Data MongoDB
- Maven Wrapper
- JUnit 5, Mockito, AssertJ, and MockMvc
ecommerce/src/
├── main/java/com/example/ecommerce/
│ ├── config/ # Shared Spring beans
│ ├── controller/ # REST endpoints
│ ├── dto/ # Validated request payloads
│ ├── model/ # MongoDB domain documents
│ ├── repository/ # Spring Data repositories
│ └── service/ # Cart, order, payment, and product behaviour
├── main/resources/ # Environment-based application configuration
└── test/ # Configuration, validation, service, and controller tests
Controllers accept HTTP requests, services coordinate domain behaviour, and Spring Data repositories persist documents in MongoDB.
- Java 17
- A MongoDB database you manage separately
Set MONGODB_URI in your shell before starting the application. PORT is optional and defaults to 8080.
export MONGODB_URI='your MongoDB connection value'
export PORT=8080Spring Boot does not automatically load JavaScript-style .env files. Use shell environment variables or configure the same variables in your IDE run configuration. Do not commit local connection values or local override property files.
From the repository root:
cd ecommerce
./mvnw clean package
./mvnw spring-boot:runStartup fails clearly if MONGODB_URI is missing.
| Method | Endpoint | Behaviour |
|---|---|---|
POST |
/api/products |
Create a product |
GET |
/api/products |
List all products |
POST |
/api/cart/add |
Add an item to a cart |
GET |
/api/cart/{userId} |
List a user's cart items |
DELETE |
/api/cart/{userId}/clear |
Clear a user's cart |
POST |
/api/orders |
Create an order from a user's cart |
GET |
/api/orders/{id} |
Retrieve an order by ID |
POST |
/api/payments/create |
Create a simulated pending payment |
POST |
/api/webhooks/payment |
Apply a SUCCESS or FAILED payment result |
Create a harmless sample product:
curl -X POST http://localhost:8080/api/products \
-H 'Content-Type: application/json' \
-d '{"name":"Notebook","description":"Sample stationery item","price":5.50,"stock":20}'Add it to a sample cart after replacing the product ID:
curl -X POST http://localhost:8080/api/cart/add \
-H 'Content-Type: application/json' \
-d '{"userId":"sample-user","productId":"PRODUCT_ID","quantity":2}'Tests use mocked repositories and services; they do not require a live MongoDB cluster. The context test uses a local-only test setting and does not perform database operations.
cd ecommerce
./mvnw test
./mvnw -DskipTests package- Supply MongoDB configuration through
MONGODB_URI; no database URL is logged by application code. - Keep credentials out of Git and rotate any value that was previously committed.
- Historical credentials should be treated as invalidated. Cleaning the current branch does not erase older Git commits.
- This coursework project does not implement authentication, authorization, webhook signatures, or production payment processing.
- No authentication or user accounts
- Product payloads have no dedicated validated DTO
- Stock updates are not transactionally coordinated with order creation
- The payment flow is a local simulation that schedules a callback to
localhost:8080 - No pagination, idempotency controls, or centralized API error model
- No integration test suite against MongoDB
There is no maintained public deployment of this coursework project.