A Spring Boot backend application for managing users and products with MongoDB integration.
- User Management: Create users with ID, name, age, email, and password
- Product Management: Full CRUD operations for products
- Display all products
- Fetch product by ID
- Add new product
- Delete product by ID
- Update product information
- Exception Handling: Proper error handling for all operations
- CLI Frontend: Simple command-line interface for interacting with the backend
- Web Interface: Modern, responsive web UI for managing users and products
- REST API: Full RESTful API endpoints for programmatic access
- MongoDB Integration: Persistent storage using MongoDB Atlas
- Java 17 or higher
- Maven 3.6 or higher
- MongoDB Atlas account (or local MongoDB instance)
-
Clone the repository (if applicable)
-
Configure MongoDB connection
Option 1: Using local properties file (Recommended)
Create
src/main/resources/application-local.propertieswith your MongoDB Atlas credentials:spring.data.mongodb.uri=mongodb+srv://admin:YOUR_PASSWORD@cluster0.swmdp.mongodb.net/user-product-management-db?retryWrites=true&w=majority
Replace
YOUR_PASSWORDwith your actual MongoDB Atlas password.Note: The
application-local.propertiesfile is already in.gitignoreto prevent committing sensitive credentials.Option 2: Using environment variable (Alternative)
You can also set the
MONGODB_URIenvironment variable:# Windows PowerShell $env:MONGODB_URI="mongodb+srv://admin:YOUR_PASSWORD@cluster0.swmdp.mongodb.net/user-product-management-db?retryWrites=true&w=majority" # Linux/Mac export MONGODB_URI="mongodb+srv://admin:YOUR_PASSWORD@cluster0.swmdp.mongodb.net/user-product-management-db?retryWrites=true&w=majority"
-
Build the project
mvn clean install
-
Run the application
For MongoDB Atlas (using local profile):
mvn spring-boot:run -Dspring.profiles.active=local
Note: You can also configure your IDE to use the
localprofile when running the application.For local MongoDB (default):
mvn spring-boot:run
The application will start on
http://localhost:8080
-
Start the backend server (as described above)
-
Open your web browser and navigate to:
http://localhost:8080 -
Navigate between sections:
- Products Tab: Manage products (view, add, edit, delete)
- Users Tab: Manage users (view, add, edit, delete)
-
Features:
- View all items: Automatically loads and displays all products or users
- Add new items: Click "Add New Product/User" button to open the form
- Edit items: Click the "Edit" button on any item card
- Delete items: Click the "Delete" button on any item card (with confirmation)
- Responsive design: Works on desktop, tablet, and mobile devices
The web interface communicates with the backend via REST API calls and provides a modern, user-friendly interface for all operations.
-
Start the backend server (as described above)
-
Run the CLI frontend (in a separate terminal):
First, ensure the project is compiled:
mvn compile
Then run the CLI using Maven:
mvn exec:java -Dexec.mainClass="com.userproduct.cli.CLIFrontend" -Dexec.classpathScope=compileNote: Make sure the backend server is running before starting the CLI.
Login Menu:
- Create User - Create a new user account
- Login - Login with an existing user ID
- Exit
Product Management Menu (after login):
- Display all products - Show all products in the repository
- Fetch product by ID - Get details of a specific product
- Add new product - Add a new product to the repository
- Delete product by ID - Remove a product from the repository
- Update product - Update an existing product's information
- Logout - Log out and return to login menu
- Exit
The application also exposes REST endpoints:
POST /api/users- Create a new userGET /api/users- Get all usersGET /api/users/{id}- Get user by IDPUT /api/users/{id}- Update a userDELETE /api/users/{id}- Delete a user
GET /api/products- Get all productsGET /api/products/{id}- Get product by IDPOST /api/products- Add a new productPUT /api/products/{id}- Update a productDELETE /api/products/{id}- Delete a product
Create User:
curl -X POST http://localhost:8080/api/users \
-H "Content-Type: application/json" \
-d '{
"id": "user1",
"name": "John Doe",
"age": 30,
"email": "john@example.com",
"password": "password123"
}'Add Product:
curl -X POST http://localhost:8080/api/products \
-H "Content-Type: application/json" \
-d '{
"id": "prod1",
"name": "Laptop",
"price": 999.99,
"expirationDate": "2025-12-31"
}'Get All Products:
curl http://localhost:8080/api/productsThe application throws appropriate exceptions:
ProductNotFoundException- When a product with the given ID doesn't existProductAlreadyExistsException- When trying to add a product with an existing ID
All exceptions are handled by the GlobalExceptionHandler and return appropriate HTTP status codes.
src/
├── main/
│ ├── java/com/userproduct/
│ │ ├── config/ # Configuration classes
│ │ ├── controller/ # REST controllers
│ │ ├── exception/ # Custom exceptions
│ │ ├── model/ # Entity models
│ │ ├── repository/ # MongoDB repositories
│ │ ├── service/ # Business logic
│ │ ├── cli/ # CLI frontend
│ │ ├── frontend/ # Frontend code (JavaFX)
│ │ └── config/ # Configuration (CORS, etc.)
│ └── resources/
│ ├── static/ # Web interface (HTML, CSS, JS)
│ │ ├── index.html
│ │ ├── styles.css
│ │ └── app.js
│ ├── application.properties
│ └── application-local.properties (create this file with your MongoDB credentials)
└── pom.xml
- Spring Boot 3.2.0
- Spring Data MongoDB
- Maven
- MongoDB Atlas
- Lombok
- Jackson (for JSON processing)
This project is for educational purposes.