ProductAPI is a RESTful backend API built with Spring Boot and MongoDB. It demonstrates a layered architecture by separating responsibilities into the Controller, Service, Repository, and Entity layers.
- RESTful CRUD API for product management
- MongoDB Atlas integration using Spring Data MongoDB
- Input validation with Jakarta Validation
- Global exception handling with custom exceptions
- Appropriate HTTP status codes for API responses
- Java 21
- Spring Boot
- Spring Data MongoDB
- MongoDB Atlas
- Maven
| Method | Endpoint | Description |
|---|---|---|
| GET | /products |
Retrieve all products |
| GET | /products/{id} |
Retrieve a product by ID |
| POST | /products |
Create a new product |
| PUT | /products/{id} |
Update an existing product |
| DELETE | /products/{id} |
Delete a product |
---
A ready-to-use Postman collection and environment are included for testing the API.
postman/
├── ProductAPI.postman_collection.json
└── ProductAPI.postman_environment.json
After importing:
- Select the ProductAPI Local environment.
- Ensure
baseUrlis set tohttp://localhost:8080. - Set the
productIdvariable to an existing product ID when testing endpoints that require one.
mvn clean package
docker build -t productapi .docker run -p 8080:8080 productapiThe API will be available at:
http://localhost:8080/products
Incoming requests are validated before processing.
namemust not be blankpricemust be greater than0
The API uses centralized exception handling to return meaningful error responses.
Example:
{
"message": "Product not found."
}Make sure you have the following installed/set up before running the project:
- Java 21
- Maven (or use the included
./mvnwwrapper) - A MongoDB Atlas account with a cluster and connection string
git clone https://github.com/SiddhiikaN/ProductAPI-SpringBoot.gitcd ProductAPI-SpringBootUpdate src/main/resources/application.properties with your MongoDB Atlas connection string before running the application.
spring.mongodb.uri = <YOUR_MONGODB_CONNECTION_STRING>./mvnw spring-boot:runor
mvn spring-boot:run