Skip to main content

Command Palette

Search for a command to run...

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

Updated
β€’4 min readβ€’View as Markdown
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
Email 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!

S
Sameer10d ago

I'm a beginner, i will try to build this. Thank you for detailed information about the project...

S

It is the perfect project to start with. Take it step-by-step. Happy Coding ❀️ You can find all resources here πŸ‘‡πŸ‘‡πŸ‘‡ https://github.com/satyajitmishra-dev/spring-boot-journey

S
Sameer9d ago

Thank you

πŸš€ Java Backend to Cloud

Part 1 of 2

ava Backend to Cloud is my public learning journey where I document everything I learn while becoming a Java Full Stack & Cloud Developer. Through real-world projects, beginner-friendly tutorials, and hands-on examples, I explore Java, Spring Boot, Spring Data JPA, MySQL, REST APIs, Spring Security, Docker, AWS, and modern backend developmentβ€”one article at a time.

Up next

πŸš€ Spring Boot Learning Journey: From Beginner to Full-Stack Java Developer

πŸš€ Spring Boot Journey #01: Building a Student Management REST API with Spring Boot, MySQL & Spring Data JPA