3.1 Spring MVC Architecture & Request Flow
← Back to Chapter 3: REST API & Web Layer
Spring MVC implements the Front Controller pattern via DispatcherServlet. Every inbound HTTP request travels through a refined pipeline of handlers, interceptors, and adapters before returning a serialized response to the client.
End-to-End Request Processing Flow
[ Client Browser / Mobile App ]
│ HTTP Request
▼
[ Filter Chain (Security, CORS, Logging) ]
│
▼
┌─────────────────────────────────────────┐
│ DispatcherServlet │
│ (The Front Controller) │
└───────┬───────────────────▲─────────────┘
│ 1. Find Handler │ 2. HandlerExecutionChain
▼ │ (Handler + Interceptors)
┌───────────────────────────┴─────────────┐
│ HandlerMapping │
│ (RequestMappingHandlerMapping) │
└─────────────────────────────────────────┘
│
│ 3. Execute Handler
▼
┌─────────────────────────────────────────┐
│ HandlerAdapter │
│ (RequestMappingHandlerAdapter) │
└───────┬───────────────────▲─────────────┘
│ Invokes method │ Returns Object or ModelAndView
▼ │
┌───────────────────────────┴─────────────┐
│ @RestController │
│ (Your Business Code) │
└─────────────────────────────────────────┘
│
│ 4. If REST (@ResponseBody):
▼
┌─────────────────────────────────────────┐
│ HttpMessageConverter │
│ (MappingJackson2HttpMessageConverter) │
└─────────────────────────────────────────┘
│ Serialized JSON
▼
[ HTTP Response 200 OK to Client ]
Core Architectural Components
DispatcherServlet: Central front controller registered with the servlet container. Coordinates all request flow.HandlerMapping: Examines request URL, HTTP method, and headers to locate the corresponding controller method.HandlerExecutionChain: Wraps the matched controller method along with all applicableHandlerInterceptorinstances.HandlerAdapter: Abstracts controller method execution; resolves method arguments (e.g., extracting@PathVariable, deserializing@RequestBody).HttpMessageConverter: Serializes Java return objects into JSON/XML (e.g., Jackson) or deserializes inbound payloads.
Filters vs HandlerInterceptors
A classic senior engineering interview comparison:
| Characteristic | Servlet Filter (Filter) | Spring Interceptor (HandlerInterceptor) |
|---|---|---|
| Specification | Standard Java EE / Jakarta Servlet API | Spring MVC proprietary framework |
| Execution Point | Outside DispatcherServlet (lowest level) | Inside DispatcherServlet, around controller execution |
| Awareness | Knows only ServletRequest & ServletResponse | Knows the target handler (HandlerMethod) and model |
| Common Use Cases | Low-level security, CORS, GZIP compression, raw payload caching | Auth validation, request audit logs, performance timing, tenant switching |
What is Statelessness in REST?
REST APIs are generally designed to be stateless.
It means:
Each request should contain the information necessary for the server to process it; the server shouldn't rely on remembering the client's previous request as conversational session state.
For example:
Request 1:
GET /customer/101
Authorization: Bearer JWT
Request 2:
GET /customer/101/orders
Authorization: Bearer JWT
Each request carries its authentication information.
This is especially useful in microservices because requests can be handled by different instances.
Rest vs Soap
| REST | SOAP |
|---|---|
| Architectural style | Protocol |
| Commonly uses JSON | Commonly uses XML |
| Lightweight | More formal/heavy |
| Easy to consume | More strict standards |
| Common in modern microservices | Common in many legacy/enterprise integrations |
| Uses HTTP commonly | Can work over multiple transports |