DEV Community

Cover image for Understanding a Real-World Spring Boot Project Structure (Best Practices)
AMIRA SAIID
AMIRA SAIID

Posted on

Understanding a Real-World Spring Boot Project Structure (Best Practices)

🧩 Understanding a Real-World Spring Boot Project Structure (Best Practices)

When you start working with Spring Boot, everything feels simple… until your project grows.

Then suddenly:

😡Too many files
πŸ˜΅β€πŸ’«Confusing structure
❌Hard to maintain code

In this article, I’ll walk you through a clean and scalable Spring Boot project structure based on a real example.

πŸ“ Project Overview

Here’s a simplified view of the structure:

src/main/java/org.sid
β”œβ”€β”€ controllers
β”œβ”€β”€ dto
β”œβ”€β”€ entities
β”œβ”€β”€ enums
β”œβ”€β”€ mappers
β”œβ”€β”€ repositories
β”œβ”€β”€ securityKeycloak
β”œβ”€β”€ services

Each layer has a clear responsibility, which makes the application maintainable and scalable.

🎯 1. Entities (entities)

Represents database tables.


🧠 2. Repository Layer (repositories)

This layer communicates with the database.

πŸ‘‰ Uses Spring Data JPA

βœ… No implementation needed β€” Spring generates it automatically!


πŸ—„οΈ 3. DTOs (dto)

DTO = Data Transfer Object
πŸ‘‰ Used to send data between layers safely

βœ… Why use DTOs?
_Protect sensitive Data:_
In a Spring Boot application, a DTO (Data Transfer Object) is used to transfer data between layers, especially between the backend and the client.
It is used to protect sensitive data: entities often contain sensitive fields such as passwords. If you return these entities directly, it can create a security risk.

Control API response:
A DTO (Data Transfer Object) allows you to decide exactly what data is sent to the client instead of exposing your full Entity.


πŸ” 4. Mappers (mappers)

Convert between Entity ↔ DTO

public class AccountMapper {
public static AccountDTO toDTO(Account account) {
// mapping logic
}
}

πŸ’‘ You can also use MapStruct for cleaner code.

πŸ”„ 5. Service Layer (services)

This is where business logic lives.

πŸ‘‰ Acts as a bridge between controller and repository

@Service
public class AccountService {

public List<AccountDTO> getAllAccounts() {
    // business logic here
}
Enter fullscreen mode Exit fullscreen mode

}
example:

βœ… Responsibilities:

-    Business rules
-    Data processing
-    Validation
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ 6.Controllers Layer (controllers)

This is the entry point of your application.

πŸ‘‰ Handles HTTP requests (GET, POST, PUT, DELETE)

Example:

βœ… Best Practice:
Keep controllers thin , No business logic here

⚠️ Important:
Do NOT expose entities directly in APIs


πŸ”’ 7. Enums (enums)

Used for fixed values like:


πŸ” 8. Security (securityKeycloak)

Handles authentication & authorization.

πŸ‘‰ In my case: Keycloak integration

Responsibilities:

  • JWT validation
  • User roles
  • Secure endpoints

πŸ”„ How Everything Works Together

Here’s the flow:


spring Boot Flow Architecture

  1. Client sends HTTP request
  2. Controller receives it
  3. Service processes logic
  4. Repository fetches data
  5. Mapper converts Entity β†’ DTO
  6. Response returned to client

πŸš€ Best Practices Recap

  βœ… Use layered architecture
  βœ… Never expose entities
  βœ… Keep controllers clean
  βœ… Use DTOs + Mappers
  βœ… Separate business logic in services
Enter fullscreen mode Exit fullscreen mode

🎯 Conclusion

A well-structured Spring Boot project is the key to building scalable and maintainable applications.

Happy coding πŸš€

Top comments (0)