Spring Boot E-Commerce API with Oracle Database
Oracle Database Express:21.3.0-xe
ref: https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
The following key entities were modeled for the e-commerce platform:
-
User: Represents a registered customer with basic profile information like
username
,password
,email
, andfirstName
. Users can have multiple orders but only one active cart. -
Product: Represents products available for purchase, with attributes like
name
,description
,price
, andquantityInStock
. This entity is related to bothOrderItem
andCartItem
. -
Cart: Represents a user's shopping cart. Each user has one cart, and a cart can have multiple
CartItems
. -
CartItem: Represents a specific product and its quantity added to the cart. It is linked to a
Product
and belongs to aCart
. -
Order: Represents a completed purchase made by a user. An order is associated with multiple
OrderItems
and a single user. The order tracks total price and status (PLACED
,CANCELLED
, orCOMPLETED
). -
OrderItem: Represents a specific product and its quantity in an order. It is linked to a
Product
and belongs to anOrder
.
User ↔ Cart: One-to-One. A user has one cart
at a time.
User ↔ Order: One-to-Many. A user can have multiple orders
.
Cart ↔ CartItem: One-to-Many. A cart can contain multiple items
.
Order ↔ OrderItem: One-to-Many. An order can contain multiple items
.
Product: Many-to-One with both CartItem
and OrderItem
.
-
Entities (Model Layer):
- The entities represent the database tables and are defined in the
model
package. - Entities include
User
,Product
,Cart
,CartItem
,Order
, andOrderItem
. - Relationships between entities are defined using JPA annotations like
@OneToMany
,@ManyToOne
, and@OneToOne
.
- The entities represent the database tables and are defined in the
-
Repository Layer:
- The
repository
package contains interfaces that extendJpaRepository
. Each repository corresponds to an entity (e.g.,UserRepository
,ProductRepository
,CartRepository
). - These repositories provide CRUD operations without the need to write boilerplate code.
- The
-
Service Layer:
- The
service
package contains service classes (e.g.,UserService
,ProductService
,CartService
,OrderService
). - Each service implements the business logic and uses repositories to interact with the database.
- Constructor injection is used in services with the help of Lombok's
@RequiredArgsConstructor
annotation, improving testability and reducing boilerplate code.
- The
-
Controller Layer:
- The
controller
package contains REST controllers that expose API endpoints to handle incoming HTTP requests. - Controllers like
UserController
,ProductController
,CartController
, andOrderController
handle user registration, product management, cart management, and order processing. - Swagger UI documentation is automatically generated for each controller.
- The
-
Exception Handling:
- Custom exceptions are defined in the
exception
package. - A global exception handler is implemented using
@ControllerAdvice
to return proper HTTP responses when an error occurs (e.g.,ResourceNotFoundException
for missing entities).
- Custom exceptions are defined in the
-
DTO (Data Transfer Objects):
- The
dto
package contains data transfer objects used to send and receive data between client and server without exposing the internal entity structure.
- The
-
Security:
- Basic JWT-based security is implemented to handle user authentication and protect endpoints.
- This can be expanded to include role-based access control (RBAC) if needed.
-
Swagger Integration: The
springdoc-openapi
library is used to automatically generate and serve API documentation. You can access it via/swagger-ui.html
to test the API directly. -
Database Configuration: The database connection is set up in
application.properties
, which contains the Oracle DB credentials and Hibernate properties.
JDK: 11
Maven: 3.9.8
Oracle Database: See Oracle Database setup
Swagger: available at http://localhost:8080/swagger-ui/
Clone the repository.
Configure the database in application.properties.
Run mvn clean install
to install dependencies.
Run mvn spring-boot:run
to start the application.
Access the Swagger at http://localhost:8080/swagger-ui/.
docker compose up -d
docker exec -it oracle-db sqlplus sys/password@XE as sysdba
SHOW PDBS;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 XEPDB1 READ WRITE NO
ALTER SESSION SET CONTAINER = XEPDB1;
CREATE USER SPRINGBOOT IDENTIFIED BY password;
GRANT ALL PRIVILEGES TO SPRINGBOOT;