RealShop API is a fully-fledged RESTful service for managing an e-commerce platform. This application allows for the management of products, cart items, and provides functionalities such as filtering, pagination, and searching. Built with Spring Boot, Spring Data JPA, and H2 Database (or any relational database), it is designed to serve as a backend for a dynamic e-commerce web or mobile application.
- Features
- Tech Stack
- Requirements
- Setup
- API Endpoints
- Request Examples
- Advanced Setup
- Testing
- Contribution Guidelines
- License
- CRUD Operations: Add, update, and delete products.
- Product Search: Search products by title (case-insensitive), price, and category.
- Filter Products: Fetch products by price, category, and more.
- Distinct Categories: Retrieve distinct categories available in the store.
- CRUD Operations: Create, update, and delete user carts.
- Item Management: Manage cart items with product IDs and quantities.
- Date Range Filtering: Filter carts based on creation date range.
- Pagination & Sorting: Fetch paginated results with sorting options for better user experience.
- Custom Exceptions: Handle errors with descriptive messages for invalid operations (e.g., product not found, cart not found).
- Response Handling: All responses are structured in a uniform way, with detailed error messages.
- Backend Framework: Spring Boot 3.4.0
- Database: H2 (default for development), MySQL/PostgreSQL (for production environments)
- ORM: Spring Data JPA
- Authentication: Optional (JWT authentication can be added)
- Documentation: OpenAPI/Swagger for API documentation (optional, can be added)
- Java 23
- Maven (or Gradle)
- IDE: IntelliJ IDEA, VS Code, Eclipse, or any preferred IDE
git clone https://github.com/NelushGayashan/RealShopAPI.git
cd RealShopAPIBy default, the project uses H2 Database for simplicity. If you're setting up for production, configure MySQL, PostgreSQL, or any other relational database in the application.properties file.
spring.datasource.url=jdbc:mysql://localhost:3306/realshop
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.DriverFor H2 (default), no configuration is needed; the app runs in memory.
- Build the project: Using Maven:
mvn clean install- Run the application:
mvn spring-boot:runThe API will be available at http://localhost:8080.
Retrieves all products with optional pagination.
Query Parameters:
page(optional): Page numbersize(optional): Number of items per pagesort(optional): Sorting criteria
Fetches a specific product by its ID.
Fetches products by category.
Search products by title (case-insensitive).
Creates a new product.
Request Body:
{
"title": "Product Name",
"price": 29.99,
"description": "Description of the product",
"image": "image_url",
"category": "Electronics"
}Updates an existing product by ID.
Request Body: Similar to the POST /products endpoint.
DELETE /products/{id} Deletes a product by its ID.
Fetch all carts with optional filters.
userId(optional): Filter by user IDstartdate(optional): Start date for creation date rangeenddate(optional): End date for creation date rangelimit(optional): Number of items to fetchsort(optional): Sorting criteria
GET /carts/{id} Fetches a specific cart by its ID.
POST /carts Creates a new cart with associated cart items. Request Body:
{
"userId": 123,
"cartItems": [
{ "productId": 1, "quantity": 2 },
{ "productId": 2, "quantity": 1 }
]
}PUT /carts/{id}
Updates an existing cart with new cart items.
Request Body: Similar to the POST /carts endpoint.
DELETE /carts/{id} Deletes a cart by its ID.
Endpoint: POST /products
Request Body:
{
"title": "Smartphone X100",
"price": 499.99,
"description": "The latest smartphone with advanced features.",
"image": "http://example.com/images/smartphone.jpg",
"category": "Electronics"
}Endpoint: POST /carts
Request Body:
{
"userId": 12345,
"cartItems": [
{
"productId": 1,
"quantity": 2
},
{
"productId": 5,
"quantity": 1
}
]
}
-
Add Dependencies:
Add Spring Security and JWT dependencies.
Example of required dependencies:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.11.2</version> </dependency>
-
JWT-Based Authentication Filter:
Implement a filter that validates and processes JWT tokens for secure API access. -
User Management Endpoints:
Create the following endpoints:- Login: For user authentication and token generation.
- Registration: For adding new users to the system.
Add the following dependency to your project:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>Configure Swagger in your Spring Boot application:
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.info(new Info().title("RealShop API").version("1.0")
.description("RealShop API documentation"));
}
}After running the application, navigate to http://localhost:8080/swagger-ui/ for interactive API documentation.
Ensure you have JUnit and Mockito dependencies in your project. Run the tests using:
mvn testCreate a personal copy of the repository.
Work on your changes in a new branch, e.g., feature/add-pagination.
Ensure new features are covered with appropriate unit tests.
Once your changes are complete, submit a pull request for review.
This project is licensed under the MIT License - see the LICENSE file for details.