Skip to content

Latest commit

ย 

History

48 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›๏ธ Spring Boot E-commerce API

Production Deploy Java Spring Boot PostgreSQL Maven

A professional-grade Spring Boot REST API for e-commerce applications, featuring complete CRUD operations, complex JPA relationships, and production-ready PostgreSQL deployment on Render.

๐ŸŒŸ Live Demo

Production API: https://spring-boot-ecommerce-api.onrender.com/

Test the API using Postman or any REST client with the endpoints documented below.

๐Ÿš€ Features

  • โœ… Complete RESTful API with full CRUD operations (GET, POST, PUT, DELETE)
  • โœ… Spring Boot 3.5.3 with Java 21 LTS
  • โœ… Production PostgreSQL database on Render
  • โœ… H2 Database for development and testing
  • โœ… Professional Architecture (Controller โ†’ Service โ†’ Repository)
  • โœ… E-commerce Domain Model with Users, Orders, Products, Categories, Payments
  • โœ… Complex JPA Relationships with proper associations and cascading
  • โœ… Order Management System with OrderItems, OrderStatus enum, and Payment tracking
  • โœ… Exception Handling with custom error responses
  • โœ… Connection Pooling with HikariCP for production
  • โœ… Environment Profiles (dev, test, prod)
  • โœ… Maven Build System with optimized dependencies
  • โœ… Production Ready with proper logging and monitoring

๐Ÿ“‹ Prerequisites

  • Java 21 or higher
  • Maven 3.6 or higher
  • PostgreSQL (for production)
  • Postman or REST client for testing

๐Ÿ› ๏ธ Installation & Setup

Local Development

  1. Clone the repository:

    git clone https://github.com/mateusribeirocampos/spring-boot-crud-api-template.git
    cd spring-boot-crud-api-template
  2. Build the project:

    mvn clean install
  3. Run locally (H2 database):

    # Switch to test profile for H2
    mvn spring-boot:run -Dspring-boot.run.profiles=test
  4. Access the application:

    • API Base URL: http://localhost:8080
    • H2 Console: http://localhost:8080/h2-console

Production Deployment

The application is configured for production deployment on Render with PostgreSQL.

๐Ÿ—„๏ธ Database Configuration

Development (H2)

  • Console: http://localhost:8080/h2-console
  • JDBC URL: jdbc:h2:mem:testdb
  • Username: sa
  • Password: (empty)

Production (PostgreSQL)

  • Database: PostgreSQL 16
  • Connection Pool: HikariCP optimized for production
  • Environment Variables: DATABASE_HOST, DATABASE_PORT, DATABASE_NAME, DATABASE_USERNAME, DATABASE_PASSWORD

๐Ÿ“ก API Endpoints

๐Ÿ”— Base URL

  • Production: https://spring-boot-ecommerce-api.onrender.com
  • Local: http://localhost:8080

๐Ÿ‘ค User Management

Method Endpoint Description Status
GET /users Get all users โœ…
GET /users/{id} Get user by ID โœ…
POST /users Create new user โœ…
PUT /users/{id} Update user โœ…
DELETE /users/{id} Delete user โœ…

๐Ÿ“ฆ Order Management

Method Endpoint Description Status
GET /orders Get all orders โœ…
GET /orders/{id} Get order by ID โœ…

๐Ÿ›๏ธ Product Management

Method Endpoint Description Status
GET /products Get all products โœ…
GET /products/{id} Get product by ID โœ…

๐Ÿ“‚ Category Management

Method Endpoint Description Status
GET /categories Get all categories โœ…
GET /categories/{id} Get category by ID โœ…

๐Ÿ“Š Sample Data

The application automatically seeds the database with comprehensive sample data through the TestConfig class:

๐Ÿ‘ฅ Users

ID Name Email Phone
1 Maria Brown [maria@gmail.com] 988888888
2 Alex Green [alex@gmail.com] 977777777

๐Ÿ“‚ Categories

ID Name
1 Electronics
2 Books
3 Computers
4 Finance
5 Games
6 Health
7 Internet
8 Education
9 Science
10 Sports

๐Ÿ›๏ธ Products

ID Name Price Categories
1 The Lord of the Rings $90.50 Books
2 Smart TV $2190.00 Electronics, Computers
3 Macbook Pro $1250.00 Computers
4 PC Gamer $1200.00 Computers
5 Rails for Dummies $100.99 Books

๐Ÿ“ฆ Orders

ID Date Status User Items
1 2025-06-20 PAID Maria Brown 2x The Lord of the Rings + 1x Macbook Pro
2 2025-07-21 WAITING_PAYMENT Alex Green 2x Macbook Pro
3 2025-07-22 WAITING_PAYMENT Maria Brown 2x Rails for Dummies
4 2025-07-07 DELIVERED Alex Green Various items

๐Ÿงช Testing with Postman

Quick Start Examples

  1. Get all users:

    GET https://spring-boot-ecommerce-api.onrender.com/users
  2. Get user by ID:

    GET https://spring-boot-ecommerce-api.onrender.com/users/1
  3. Create new user:

    POST https://spring-boot-ecommerce-api.onrender.com/users
    Content-Type: application/json
    
    {
      "name": "Joรฃo Silva",
      "email": "joao@email.com",
      "phone": "999999999",
      "password": "123456"
    }
  4. Get all products:

    GET https://spring-boot-ecommerce-api.onrender.com/products-รง
  5. Get order details:

    GET https://spring-boot-ecommerce-api.onrender.com/orders/1

๐Ÿ—๏ธ Project Architecture

๐Ÿ“ Directory Structure

src/
โ”œโ”€โ”€ main/
โ”‚   โ”œโ”€โ”€ java/com/campos/backend/
โ”‚   โ”‚   โ”œโ”€โ”€ BackendApplication.java          # ๐Ÿš€ Main application class
โ”‚   โ”‚   โ”œโ”€โ”€ config/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ TestConfig.java              # โš™๏ธ Database seeding configuration
โ”‚   โ”‚   โ”œโ”€โ”€ entities/                        # ๐Ÿ“Š JPA Entities
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ User.java                    # ๐Ÿ‘ค User entity
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Order.java                   # ๐Ÿ“ฆ Order entity
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Product.java                 # ๐Ÿ›๏ธ Product entity
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Category.java                # ๐Ÿ“‚ Category entity
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ OrderItem.java               # ๐Ÿ“ Order item entity
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Payment.java                 # ๐Ÿ’ณ Payment entity
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ enums/
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ OrderStatus.java         # ๐Ÿ“‹ Order status enum
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ pk/
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ OrderItemPK.java         # ๐Ÿ”‘ Composite primary key
โ”‚   โ”‚   โ”œโ”€โ”€ repositories/                    # ๐Ÿ—„๏ธ Data Access Layer
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserRepository.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ OrderRepository.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ProductRepository.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ CategoryRepository.java
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ OrderItemRepository.java
โ”‚   โ”‚   โ”œโ”€โ”€ resources/                       # ๐ŸŒ REST Controllers
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserResource.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ OrderResource.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ProductResource.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ CategoryResource.java
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ exceptions/                  # โš ๏ธ Exception handlers
โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ ResourceExceptionHandler.java
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ StandardError.java
โ”‚   โ”‚   โ””โ”€โ”€ services/                        # ๐Ÿ’ผ Business Logic Layer
โ”‚   โ”‚       โ”œโ”€โ”€ UserService.java
โ”‚   โ”‚       โ”œโ”€โ”€ OrderService.java
โ”‚   โ”‚       โ”œโ”€โ”€ ProductService.java
โ”‚   โ”‚       โ”œโ”€โ”€ CategoryService.java
โ”‚   โ”‚       โ””โ”€โ”€ exceptions/                  # โš ๏ธ Custom exceptions
โ”‚   โ”‚           โ”œโ”€โ”€ DatabaseException.java
โ”‚   โ”‚           โ””โ”€โ”€ ResourceNotFoundException.java
โ”‚   โ””โ”€โ”€ resources/
โ”‚       โ”œโ”€โ”€ application.properties           # ๐Ÿ”ง Production config
โ”‚       โ”œโ”€โ”€ application-prod.properties      # ๐Ÿญ Production config
โ”‚       โ””โ”€โ”€ application-test.properties      # ๐Ÿงช Test config
โ””โ”€โ”€ test/
    โ””โ”€โ”€ java/com/campos/backend/
        โ””โ”€โ”€ BackendApplicationTests.java     # โœ… Test class

๐Ÿ”ง Environment Configuration

Production Profile (application-prod.properties)

# PostgreSQL Database
spring.datasource.url=jdbc:postgresql://${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}
spring.datasource.username=${DATABASE_USERNAME}
spring.datasource.password=${DATABASE_PASSWORD}

# HikariCP Connection Pool
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.minimum-idle=5

# JPA Configuration
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=false

Test Profile (application-test.properties)

# H2 In-Memory Database
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=

# JPA Configuration
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true

๐Ÿ“ฆ Key Dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
</dependencies>

๐Ÿงช Testing

Run Tests

mvn test

Build & Package

mvn clean install

Run with Different Profiles

# Test profile (H2 database)
mvn spring-boot:run -Dspring-boot.run.profiles=test

# Production profile (PostgreSQL)
mvn spring-boot:run -Dspring-boot.run.profiles=prod

๐Ÿ›๏ธ Architecture & Design Patterns

๐Ÿ”„ Layered Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                          ๐ŸŒ REST Layer                              โ”‚
โ”‚                    (UserResource, OrderResource)                    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                         ๐Ÿ’ผ Service Layer                            โ”‚
โ”‚                   (UserService, OrderService)                       โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                       ๐Ÿ—„๏ธ Repository Layer                           โ”‚
โ”‚                 (UserRepository, OrderRepository)                   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                        ๐Ÿ“Š Entity Layer                              โ”‚
โ”‚                      (User, Order, Product)                         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                        ๐Ÿ˜ Database Layer                            โ”‚
โ”‚                        (PostgreSQL / H2)                            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“Š Domain Model

Entity Description Key Features
User System users Personal info, order history
Order Customer orders Timestamps, status tracking
Product Items for sale Pricing, categorization
Category Product categories Many-to-many relationships
OrderItem Order-Product association Quantity, pricing
Payment Payment information One-to-one with orders

๐Ÿ”— JPA Relationships

  • OneToMany: User โ†’ Orders
  • ManyToOne: Order โ†’ User
  • ManyToMany: Product โ†” Category
  • ManyToMany with Extra Attributes: Order โ†” Product (via OrderItem)
  • OneToOne: Order โ†” Payment

๐Ÿ“‹ Order Status Management

Status Description
WAITING_PAYMENT Order created, awaiting payment
PAID Payment processed successfully
SHIPPED Order shipped to customer
DELIVERED Order delivered to customer
CANCELED Order canceled

๐Ÿš€ Production Features

โœ… What's Implemented

  • โœ… Full CRUD operations for Users
  • โœ… Read operations for Orders, Products, Categories
  • โœ… Complex JPA relationships
  • โœ… Exception handling
  • โœ… Production PostgreSQL deployment
  • โœ… Connection pooling with HikariCP
  • โœ… Environment-specific configurations
  • โœ… Comprehensive sample data

๐Ÿ”ฎ Future Enhancements

  • ๐Ÿ”„ Complete CRUD for all entities
  • ๐Ÿ” Authentication & authorization (Spring Security)
  • ๐Ÿ“Š API documentation (Swagger/OpenAPI)
  • ๐Ÿงช Comprehensive testing suite
  • ๐Ÿ“ˆ Monitoring and metrics
  • ๐Ÿ” Advanced search and filtering
  • ๐Ÿ“ Input validation
  • ๐Ÿ’ฐ Payment processing integration

๐Ÿค Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository

  2. Create a feature branch

    git checkout -b feature/amazing-feature
  3. Commit your changes

    git commit -m 'Add some amazing feature'
  4. Push to the branch

    git push origin feature/amazing-feature
  5. Open a Pull Request

๐Ÿ“ License

This project is open source and available under the MIT License.

๐Ÿ‘ค Author

Mateus R Campos


โญ Star this repository if you find it helpful!

๐Ÿš€ Live API: https://spring-boot-ecommerce-api.onrender.com/

๐Ÿ“ง Questions? Feel free to reach out via LinkedIn or create an issue in this repository.

About

A RESTful API built with Java, Spring Boot, Maven, and PostgreSQL, featuring CRUD operations, test database with H2, and Render deployment.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages