Prerequisites
Before you begin, ensure you have the following installed on your system:Java 17
Required for Spring Boot 4.0.1. Verify with
java -versionMaven
Build tool for dependency management. Included via Maven Wrapper (mvnw)
Database
PostgreSQL (production) or H2 (development, included by default)
Git
For cloning the repository
This guide uses H2 in-memory database for quick setup. For production deployment with PostgreSQL, see the Setup Guide.
Setup Instructions
Clone the Repository
First, clone the source code to your local machine:The project structure includes:
src/main/java- Application source codesrc/main/resources- Configuration filespom.xml- Maven dependencies
Configure Application Properties
The default configuration in
src/main/resources/application.properties is ready to use:Optional: Configure PostgreSQL
Optional: Configure PostgreSQL
To use PostgreSQL instead of H2, add these properties:
Build the Application
Use Maven to download dependencies and compile the project:This command:
- Downloads all required dependencies (Spring Boot, Hibernate, PostgreSQL driver, etc.)
- Compiles Java source files
- Runs tests
- Packages the application as a JAR file
Run the Application
Start the Spring Boot application:You should see output indicating the application has started:The API server is now running at
http://localhost:8080The
DatabasePreLoader component automatically creates sample data:- 2 test users ([email protected], [email protected])
- 2 products (Laptop, Smartphone) in the Electronics category
Make Your First API Request
Now that the API is running, let’s retrieve the product catalog.Get All Products
Response Structure
The API returns a paginated, HATEOAS-compliant response:Understanding the Response
Understanding the Response
HATEOAS Links: Each resource includes
_links with URLs to related resources and actions.Pagination Metadata: The page object contains:size: Items per page (default: 20, max: 50)totalElements: Total number of productstotalPages: Number of pages availablenumber: Current page index (0-based)
ProductView class:- Basic info: id, name, description
- Pricing: price field (BigDecimal for precision)
- Inventory: stockQuantity
- Categories: Array of associated categories
- Timestamps: createdAt, updatedAt (ISO 8601 format)
Additional API Operations
Get a Specific Product
Get All Categories
Get All Users
UserView without password).
Pagination Parameters
Control pagination with query parameters:Page numbers are 0-indexed. The first page is
page=0.Create a New Product
Let’s add a new product to the catalog:Success Response
The API returns HTTP 201 Created with the new product:Categories are created automatically if they don’t exist. The “Audio” category is created during this request since it didn’t exist before.
Update a Product
Modify existing product attributes using PATCH or PUT:updatedAt timestamp is automatically refreshed.
Delete a Product
Remove a product from the catalog:Next Steps
Now that you have the API running, explore these topics:API Reference
Complete documentation of all endpoints, request/response formats, and error codes
Authentication
Learn how to implement user authentication and secure your API requests
Shopping Cart
Manage shopping carts with automatic discount calculation
Pagination Guide
Use pagination, sorting, and filtering to efficiently query large datasets
Troubleshooting
Port 8080 is already in use
Port 8080 is already in use
Change the port in Or set it via command line:
application.properties:Database connection errors
Database connection errors
If using PostgreSQL, verify:
- PostgreSQL service is running
- Database credentials in
application.propertiesare correct - Database exists (create it with
CREATE DATABASE ecommerce;)
Maven build fails
Maven build fails
Common solutions:
- Verify Java 17 is installed:
java -version - Clear Maven cache:
./mvnw clean - Delete
~/.m2/repositoryif dependencies are corrupted - Check internet connection for dependency downloads
Sample data not loading
Sample data not loading
The
DatabasePreLoader runs automatically on startup. If data isn’t present:- Check console logs for errors during preloading
- Verify database schema was created (check logs for Hibernate DDL statements)
- Try with a fresh H2 database (delete any existing database files)