Clean β’ Professional
The DispatcherServlet is the Front Controller of Spring MVC.
Every HTTP request in a Spring Boot / Spring MVC application passes through it. It coordinates request handling, controller invocation, view resolution, and response generation.
Understanding its internal flow is essential for debugging request issues, customizing MVC behavior, and mastering Spring Boot internals.
DispatcherServlet is a central dispatcher that:
It implements the Front Controller design pattern and provides a centralized entry point for Spring MVC applications.
Client Request
β
DispatcherServlet
β
HandlerMapping
β
HandlerAdapter
β
Controller Execution
β
ReturnValue /Exception
β
ViewResolverOR HttpMessageConverter
β
HTTP Response
@RequestMapping, @GetMappingExample:
@GetMapping("/users")
public List<User>getUsers() { }
Common adapters:
| Adapter | Purpose |
|---|---|
RequestMappingHandlerAdapter | Invokes annotated controller methods |
HttpRequestHandlerAdapter | Handles HttpRequestHandler implementations |
This abstraction allows multiple controller styles to coexist seamlessly.
@Valid)ModelAndViewString)@ResponseBody / REST objectResponseEntityExample:
"user" βuser.html (Thymeleaf)
Common ViewResolvers:
| Resolver | Usage |
|---|---|
InternalResourceViewResolver | JSP views |
ThymeleafViewResolver | Thymeleaf templates |
FreeMarkerViewResolver | FreeMarker templates |
HttpMessageConverter converts objects β JSON/XMLExample (REST):
@ResponseBody
public UsergetUser(@PathVariable Long id) {return user; }
| Response Component | Description |
|---|---|
| Status code | 200, 404, 500 etc. |
| Headers | Content-Type, Content-Disposition |
| Body | HTML / JSON / XML |
| Method | Trigger |
|---|---|
preHandle() | Before controller execution |
postHandle() | After controller execution |
afterCompletion() | After view rendering / response |
Common uses:
Interceptors are Spring MVCβspecific (different from servlet filters).
@ExceptionHandler@ControllerAdviceHandlerExceptionResolver| Feature / Aspect | MVC Flow | REST Flow |
|---|---|---|
| Purpose | Render server-side HTML pages for browsers | Provide data (JSON/XML) to clients |
| Controller Return Type | ModelAndView, String (view name) | Java object, ResponseEntity, @ResponseBody |
| View Resolution | Uses ViewResolver to map logical view β template | Skipped; no template rendering |
| Response Format | HTML | JSON / XML / other serialized formats |
| Message Conversion | Optional (HttpMessageConverter) | Mandatory (HttpMessageConverter) |
| Client Type | Browser, rendering engine | SPA, mobile apps, API clients |
| Use Case | Web applications, server-side pages | RESTful APIs, microservices |
| Example Controller | java @GetMapping("/users") public String getUsers(Model model) { return "user-list"; } | java @GetMapping("/users") public List<User> getUsers() { return userService.findAll(); } |
| Rendering Logic | Server prepares model + HTML | Server sends raw data, client handles rendering |
| Interceptor & Exception Flow | Applies equally to both MVC and REST | Same interceptors and exception handling |
| Performance Consideration | Slightly heavier due to view rendering | Lightweight, data-only responses |
| Component | Responsibility |
|---|---|
| DispatcherServlet | Acts as the Front Controller; receives all HTTP requests and orchestrates processing. |
| HandlerMapping | Determines which controller should handle the incoming request based on URL, HTTP method, and annotations. |
| HandlerAdapter | Knows how to invoke the controller method and resolve its arguments. |
| Controller | Contains the business logic for processing requests. |
| Interceptor | Provides cross-cutting functionalities like logging, authentication, and metrics via preHandle, postHandle, afterCompletion. |
| ViewResolver | Resolves logical view names to actual templates (like Thymeleaf, JSP, or FreeMarker) β used in MVC only. |
| HttpMessageConverter | Converts Java objects to JSON, XML, or other formats for REST responses; also converts request bodies to Java objects. |
| ExceptionResolver | Handles exceptions consistently using @ExceptionHandler, @ControllerAdvice, or custom resolvers. |
The DispatcherServlet Internal Processing Flow is the backbone of Spring MVC. Understanding it allows developers to: