5.1 ~ 5.3 Understanding Spring MVC
1. Client-Server Architecture & HTTP Refresher
The web revolves around two roles:
- Client (Browser/App): The one that sends requests (e.g., typing a URL in Chrome or clicking a button in a frontend app)
- Server (Spring Boot App): The one that receives, processes, and responds
The protocol governing their communication is HTTP (HyperText Transfer Protocol).
Client Server (Spring Boot)
[Browser] ---HTTP Request---> [Controller]
<--HTTP Response--- [Returns result]
HTTP Request/Response Structure
| Component | Description | Example |
|---|---|---|
| URL | Where to send the request | GET /api/users/1 |
| Method | What operation to perform | GET, POST, PUT, DELETE |
| Header | Metadata about the request | Content-Type: application/json |
| Body | Data being sent (POST, PUT, etc.) | {"name": "John"} |
| Status Code | Result of the response | 200 OK, 404 Not Found |
2. The MVC Architecture Pattern
MVC (Model-View-Controller) is a design pattern that separates the application into three distinct responsibilities.
Request → [Controller] → [Service/Model] → [View or JSON Response]
| Layer | Responsibility | Actual Code |
|---|---|---|
| Model | Data and business logic | UserService, UserRepository, User Entity |
| View | What the user sees | REST API → ** JSON response**(or Thymeleaf HTML) |
| Controller | Receives requests, calls Model, determines the View | @RestController class |
Why Separate into MVC?
- Clear Responsibility: Each layer focuses solely on its own role.
- Easier Maintenance: Changing DB logic doesn't require touching the Controller.
- Easier Testing: Each layer can be independently unit-tested.
3. DispatcherServlet & the Full Request Lifecycle
The true heart of Spring MVC is a single DispatcherServlet, acting as the ** Front Controller**. Every HTTP request must pass through it first.
Full Request Processing Flow
[HTTP Request]
↓
[DispatcherServlet] (Central hub managed by Spring)
↓
[HandlerMapping] (Finds the correct Controller method for the URL)
↓
[Controller Method Executes] (@GetMapping, @PostMapping, etc.)
↓
[Service → Repository → DB] (Business logic and data access)
↓
[Returns Result] (REST API → Serialized to JSON / MVC → ViewResolver)
↓
[HTTP Response]
DispatcherServlet's Collaborators
| Collaborator | Role |
|---|---|
| HandlerMapping | Determines which Controller handles which URL |
| HandlerAdapter | Actually invokes the Controller method |
| ViewResolver | Maps a view name to a physical template file (HTML, etc.) |
| MessageConverter | Converts Java objects to JSON (using Jackson) |
Simplified for REST API Development: When using
@RestController, the ViewResolver is bypassed entirely. Instead, ** MessageConverter (Jackson)**directly serializes the returned Java object into JSON and writes it to the HTTP response body. Understanding this flow gives you the complete picture of how Spring Boot works.
Key Takeaways
- Every HTTP request is first received by DispatcherServlet.
- The Controller acts as the entry point and delegates business logic to the Service.
- The Service applies business rules and accesses the database through the Repository.
- REST APIs ultimately respond with data in JSON format.
This layered architecture (Controller - Service - Repository) is the backbone of real-world Spring Boot development.