Build an Employee Management System with Spring Boot: DTO, Validation, Exception Handling & JPA Explained (Beginner Friendly)

π Learn Spring Boot by Building an Employee Management System
"Have you ever wondered how companies manage thousands of employee records without getting lost in spreadsheets?"
Every company, from startups to large enterprises, needs a system to manage employee data efficiently.
In this article, we'll build an Employee Management System using Spring Boot, MySQL, and Spring Data JPA while learning concepts like DTOs, validation, exception handling, and soft delete.
π What Are We Building?
We are building a REST API that can:
- β Create employees
- β Get all employees
- β Get employee by ID
- β Update employee details
- β Filter employees by department
- β Soft delete employees
- β Validate user input
- β Handle exceptions globally
π οΈ Tech Stack
| Technology | Purpose |
|---|---|
| Java 21 | Programming language |
| Spring Boot | Backend framework |
| MySQL | Database |
| Spring Data JPA | Database operations |
| Bean Validation | Input validation |
| Maven | Dependency management |
ποΈ Project Architecture
A clean project structure makes your code easier to maintain.
Client
β
Controller
β
Service
β
Repository
β
Database
Controller
Handles HTTP requests and returns responses.
Service
Contains all business logic.
Repository
Communicates with the database.
DTO
Transfers data safely between the client and server.
π Database Schema
| Field | Example |
|---|---|
| Name | Aman Sharma |
| aman@gmail.com | |
| Department | IT |
| Designation | Software Engineer |
| Salary | βΉ50,000 |
π₯ API Endpoints
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/employee |
Get all employees |
| GET | /api/v1/employee/{id} |
Get employee by ID |
| POST | /api/v1/employee |
Create employee |
| PUT | /api/v1/employee/{id} |
Update employee |
| PATCH | /api/v1/employee/{id} |
Soft delete employee |
| GET | /api/v1/employee/filter?department=IT |
Filter by department |
π₯ Sample Request
{
"name": "Aman Sharma",
"email": "aman@gmail.com",
"department": "IT",
"designation": "Software Engineer",
"salary": 50000
}
π¦ Understanding DTO
DTO (Data Transfer Object) is used to transfer data between the client and the server.
Think of it like an employee ID card.
The company database contains a lot of information, but the ID card only shows the necessary details.
Why use DTO?
- Keeps APIs clean
- Hides internal fields
- Improves security
- Makes validation easier
β Validation
Validation ensures that invalid data never reaches the database.
β Invalid request
{
"name": "",
"email": "abc"
}
β Valid request
{
"name": "Rahul Sharma",
"email": "rahul@gmail.com"
}
Validation rules
- Name cannot be empty
- Email must be valid
- Salary must be greater than 100
- Department cannot be blank
β οΈ Global Exception Handling
Instead of returning confusing errors, we send meaningful messages.
Resource not found
{
"message": "Employee not found with id: 10"
}
Duplicate email
{
"message": "Email already exists"
}
Validation error
{
"email": "Invalid email format"
}
ποΈ What Is Soft Delete?
Normally, deleting data removes it permanently.
Soft delete simply marks the record as deleted.
employee.setDeleted(true);
Benefits
- Data is not lost permanently
- Easy to restore
- Better auditing
- Fewer database issues
π§ Challenges I Faced
As a beginner, I made several mistakes:
- Returning entities directly
- Forgetting validation
- Writing business logic inside controllers
- Ignoring exception handling
- Showing deleted records
These mistakes helped me understand how enterprise applications are built.
π What I Learned
After completing this project, I learned:
- DTOs
- Validation
- Exception handling
- Spring Data JPA
- REST API design
- Layered architecture
- Soft delete
π Future Improvements
- Pagination and sorting
- Swagger documentation
- Spring Security
- Role-based authentication
- Unit testing
π― Conclusion
This project is much more than a simple CRUD application.
It teaches you how to build clean, maintainable, and production-ready backend applications using Spring Boot.
If you're a beginner, try building this project yourself. You'll learn much more than by simply watching tutorials.
β If you found this article helpful, don't forget to share it!



