A RESTful backend service for an Online Food Ordering System, built with Java and Spring Boot. The system enables administrators to manage a catalog of food items and discounts, while users can browse available food, manage a shopping cart, and place orders.
The application is structured around a clean, layered architecture (controllers β services β persistence adapters β JPA repositories) and secured with JWT-based authentication and role-based authorization.
- Features
- Tech Stack
- Prerequisites
- Getting Started
- Database Setup
- API Endpoints Overview
- Project Structure
- License
- Manage food items β create, read, update, and delete food items (single or in bulk).
- Manage discounts β create and update discounts, and apply a discount to multiple food items at once.
- Browse catalog β paginated and sortable listing of all food items.
- Register & log in β account registration and JWT-based authentication.
- Browse food items β view available food items with pagination and sorting.
- Shopping cart β add items to the cart, update quantities, view cart contents, see the cart total, and remove one, many, or all items.
- Place orders β convert the current cart into an order.
- Stateless authentication using JSON Web Tokens (JWT).
- Role-based access control with
USERandADMINroles. - Public endpoints (registration, login, health checks) and protected endpoints separated via configurable security paths.
| Category | Technology |
|---|---|
| Language | Java 21 |
| Framework | Spring Boot 3.3.4 |
| Spring Modules | Spring Web, Spring Data JPA, Spring Security, Spring Actuator |
| Authentication | JWT (jjwt 0.12.6) |
| Database | MySQL 8 (SQL) |
| ORM / Persistence | Hibernate / JPA |
| Object Mapping | MapStruct 1.5.5 |
| Boilerplate | Lombok |
| Build Tool | Gradle |
Before running the project, make sure you have the following installed:
- Java Development Kit (JDK) 21 or higher
- MySQL 8.x server (running locally or remotely)
- Git
- (Optional) Gradle β the project ships with the Gradle Wrapper (
gradlew), so a local Gradle installation is not required.
git clone https://github.com/<your-username>/online-food-order-be.git
cd online-food-order-beCreate the MySQL database and update the credentials in
src/main/resources/application.properties (see Database Setup below).
Using the Gradle Wrapper:
# On macOS / Linux
./gradlew bootRun
# On Windows
gradlew.bat bootRunTo build an executable JAR instead:
./gradlew clean build
java -jar build/libs/backend-0.0.1-SNAPSHOT.jarThe application starts on http://localhost:8080 by default.
curl http://localhost:8080/actuator/health
# {"status":"UP"}The application connects to a MySQL database named online_food_order. Hibernate is configured with ddl-auto=update, so the schema (tables) is created and updated automatically on startup.
CREATE DATABASE online_food_order;The default configuration lives in src/main/resources/application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/online_food_order
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8DialectUpdate spring.datasource.username and spring.datasource.password to match your local MySQL credentials.
β οΈ Security note: The repository ships with a sample JWT secret and database credentials for local development only. Never commit real secrets. For production, externalize sensitive values using environment variables or a secrets manager.
All endpoints are prefixed with /api/v1. Protected endpoints require an Authorization: Bearer <token> header.
| Method | Endpoint | Description | Auth |
|---|---|---|---|
POST |
/api/v1/idm/register |
Register a new user account | Public |
POST |
/api/v1/idm/login |
Authenticate and receive a JWT token | Public |
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/v1/food |
Create a new food item |
GET |
/api/v1/food/{id} |
Get a single food item by ID |
GET |
/api/v1/food |
List all food items (paginated & sortable) |
PUT |
/api/v1/food/{id} |
Update an existing food item |
DELETE |
/api/v1/food/{id} |
Delete a single food item |
DELETE |
/api/v1/food |
Delete multiple food items (list of IDs in the body) |
Listing supports the query parameters
page,size,sortBy, andsortDirection(e.g.?page=0&size=10&sortBy=createdAt&sortDirection=desc).
| Method | Endpoint | Description |
|---|---|---|
PUT |
/api/v1/cart |
Add a food item to the cart (foodItemId, quantity) |
PUT |
/api/v1/cart/update |
Update the quantity of a cart item |
GET |
/api/v1/cart/cart-items |
Get all items currently in the cart |
GET |
/api/v1/cart/total |
Get the number of items in the cart |
DELETE |
/api/v1/cart/item/{foodCartId} |
Remove a single item from the cart |
DELETE |
/api/v1/cart/items |
Remove all items from the cart |
DELETE |
/api/v1/cart |
Remove multiple cart items (list of IDs in the body) |
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/v1/order |
Place an order from the current cart |
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/v1/discount |
Create a new discount |
PUT |
/api/v1/discount/{id} |
Update an existing discount |
GET |
/api/v1/discount/{id} |
Get a single discount by ID |
GET |
/api/v1/discount |
List all discounts (paginated & sortable) |
PUT |
/api/v1/discount/update-food-items-discounts |
Apply a discount to multiple food items |
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/v1/users/{id} |
Get a user by ID |
| Method | Endpoint | Description | Auth |
|---|---|---|---|
GET |
/actuator/health |
Application health check | Public |
GET |
/actuator/info |
Application info | Public |
The codebase follows a layered, ports-and-adapters style architecture:
online-food-order-be/
βββ build.gradle # Gradle build configuration & dependencies
βββ settings.gradle
βββ gradlew / gradlew.bat # Gradle wrapper scripts
βββ src/
βββ main/
β βββ java/com/foodorder/backend/
β β βββ OnlinefoodorderApplication.java # Application entry point
β β βββ application/ # Web / API layer
β β β βββ controllers/ # REST controllers (Food, Cart, Order, Discount, Idm, SysUser)
β β β βββ dtos/ # Request/response DTOs
β β β βββ exception/ # Custom exceptions & global handler
β β βββ configration/
β β β βββ security/ # Spring Security & JWT filter config
β β βββ domain/ # Business / domain layer
β β β βββ enums/ # System enums (e.g. roles)
β β β βββ handlers/ # Auth entry point & access-denied handlers
β β β βββ mappers/ # MapStruct mappers
β β β βββ model/ # Domain models
β β β βββ providers/ # Identity provider
β β β βββ services/ # Business logic & security services
β β βββ persistence/ # Persistence layer
β β βββ adapter/ # Repository adapters
β β βββ entity/ # JPA entities
β β βββ jpa/ # Spring Data JPA repositories
β β βββ repository/ # Repository ports (interfaces)
β βββ resources/
β βββ application.properties # Main configuration
β βββ application-sqllogs.properties # SQL logging profile
βββ test/
βββ java/com/foodorder/backend/ # Tests
Layer responsibilities:
applicationβ exposes REST endpoints, validates input, and maps between DTOs and domain models.domainβ holds business logic, domain models, mappers, and security services.persistenceβ translates domain models to JPA entities and persists them via Spring Data repositories.configrationβ wires up Spring Security, the JWT authentication filter, and related beans.
This project is licensed under the MIT License. See the LICENSE file for details.
If you intend to publish this repository publicly, add a
LICENSEfile at the project root containing the full MIT license text (or your license of choice).