Layered Architecture
Spring Boot applications commonly follow a layered architecture to organize code into distinct functional modules. This pattern promotes modularity, maintainability, testability, and separation of concerns, making applications easier to develop, test, and scale.
The most common implementation divides the application into three main layers:

1. Controller Layer (Presentation Layer)
- Acts as the entry point of the application, handling HTTP requests from clients (browser, mobile apps, API consumers).
- Responsible for request validation, input handling, authentication, and delegating business logic to the Service layer.
- Uses annotations:
@Controlleror@RestController. - Converts JSON to Java objects and vice versa for REST APIs.
Example:
@RestController
@RequestMapping("/users")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
return ResponseEntity.ok(userService.getUserById(id));
}
}
2. Service Layer (Business Logic Layer)
- Contains the core business logic of the application.
- Orchestrates data flow between the Controller and Repository layers.
- Applies validation, authorization, and other business rules.
- Annotated with
@Service.
Example:
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User getUserById(Long id) {
return userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
}
}
3. Repository Layer (Data Access Layer)
- Manages database operations, including CRUD operations and queries.
- Abstracts database interactions using Spring Data JPA, Hibernate, JDBC, or other persistence frameworks.
- Annotated with
@Repository.
Example:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
How the Layers Interact
- Client Request: A client sends an HTTP request (e.g., GET /users/1).
- Controller: Receives the request, validates input, and delegates processing to the Service layer.
- Service Layer: Executes business logic and calls the Repository layer for data operations.
- Repository Layer: Interacts with the database and returns results.
- Service Layer: Processes the results if needed and returns to Controller.
- Controller: Formats the response and sends it back to the client.
How the Layers Interact - Diagram
Client (Browser / Postman)
│
▼
Controller Layer (@RestController)
│
▼
Service Layer (@Service – Business Logic)
│
▼
Repository Layer (@Repository – Data Access)
│
▼
Database
▲
│
←←←←←←←←←←← JSON Response ←←←←←←←←←←
Flow Summary:
- Client sends a request.
- Controller receives and validates it, then calls the Service layer.
- Service layer applies business logic and calls the Repository layer for database operations.
- Repository interacts with the database and returns data.
- Service processes the data and sends it back to Controller.
- Controller formats the response and sends it to the client.
Benefits of Layered Architecture
- Separation of Concerns: Each layer focuses on a specific responsibility.
- Maintainability: Changes in one layer (e.g., switching the database) have minimal impact on other layers.
- Reusability: Business logic in the Service layer can be reused across multiple controllers.
- Testability: Each layer can be unit-tested independently.
- Scalability: Supports adding new features or layers without disrupting existing functionality.
Conclusion
The Controller-Service-Repository layered architecture in Spring Boot ensures a clean, modular, and maintainable structure for web applications. By separating concerns, developers can easily manage business logic, database interactions, and presentation logic, resulting in robust, scalable, and testable applications.
