Skip to main content

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​

  1. DispatcherServlet: Central front controller registered with the servlet container. Coordinates all request flow.
  2. HandlerMapping: Examines request URL, HTTP method, and headers to locate the corresponding controller method.
  3. HandlerExecutionChain: Wraps the matched controller method along with all applicable HandlerInterceptor instances.
  4. HandlerAdapter: Abstracts controller method execution; resolves method arguments (e.g., extracting @PathVariable, deserializing @RequestBody).
  5. HttpMessageConverter: Serializes Java return objects into JSON/XML (e.g., Jackson) or deserializes inbound payloads.

Filters vs HandlerInterceptors​

A classic senior engineering interview comparison:

CharacteristicServlet Filter (Filter)Spring Interceptor (HandlerInterceptor)
SpecificationStandard Java EE / Jakarta Servlet APISpring MVC proprietary framework
Execution PointOutside DispatcherServlet (lowest level)Inside DispatcherServlet, around controller execution
AwarenessKnows only ServletRequest & ServletResponseKnows the target handler (HandlerMethod) and model
Common Use CasesLow-level security, CORS, GZIP compression, raw payload cachingAuth 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​

RESTSOAP
Architectural styleProtocol
Commonly uses JSONCommonly uses XML
LightweightMore formal/heavy
Easy to consumeMore strict standards
Common in modern microservicesCommon in many legacy/enterprise integrations
Uses HTTP commonlyCan work over multiple transports