Skip to main content
Advertisement

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

ComponentDescriptionExample
URLWhere to send the requestGET /api/users/1
MethodWhat operation to performGET, POST, PUT, DELETE
HeaderMetadata about the requestContent-Type: application/json
BodyData being sent (POST, PUT, etc.){"name": "John"}
Status CodeResult of the response200 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]
LayerResponsibilityActual Code
ModelData and business logicUserService, UserRepository, User Entity
ViewWhat the user seesREST API → ** JSON response**(or Thymeleaf HTML)
ControllerReceives 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

CollaboratorRole
HandlerMappingDetermines which Controller handles which URL
HandlerAdapterActually invokes the Controller method
ViewResolverMaps a view name to a physical template file (HTML, etc.)
MessageConverterConverts 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

  1. Every HTTP request is first received by DispatcherServlet.
  2. The Controller acts as the entry point and delegates business logic to the Service.
  3. The Service applies business rules and accesses the database through the Repository.
  4. REST APIs ultimately respond with data in JSON format.

This layered architecture (Controller - Service - Repository) is the backbone of real-world Spring Boot development.

Advertisement