What is the DispatcherServlet and how does the Spring MVC request flow work?
Learn what the DispatcherServlet is and how the Spring MVC request flow works through HandlerMapping, controllers, adapters, and view resolvers, with examples.
Expected Interview Answer
The DispatcherServlet is Spring MVC's front controller: a single servlet that receives every incoming HTTP request and orchestrates the components that handle it, map it to a controller, and build the response.
On a request it consults a HandlerMapping to find the matching controller method, invokes it through a HandlerAdapter, and applies any registered HandlerInterceptors before and after. The controller returns data or a view name; for views a ViewResolver resolves it and the view renders the model, while @ResponseBody/@RestController responses are serialized by an HttpMessageConverter. Exceptions are routed to HandlerExceptionResolvers. In Spring Boot the DispatcherServlet is auto-configured and mapped to '/' by default.
- Single entry point centralizes request handling
- Decouples routing, controllers, and views
- Enables consistent cross-cutting logic via interceptors
- Supports both view rendering and REST responses
- Auto-configured in Spring Boot with sensible defaults
AI Mentor Explanation
Think of the team captain who receives every ball situation and decides which fielder or bowler should respond, coordinating the whole side. The DispatcherServlet is that captain for HTTP requests: every request comes to it first, and it delegates to the right controller, adapter, and view before the response goes back out.
Step-by-Step Explanation
Step 1
Request arrives
The DispatcherServlet, mapped to '/', receives the incoming HTTP request as the front controller.
Step 2
Find the handler
It asks a HandlerMapping which controller method matches the URL and HTTP method.
Step 3
Invoke via adapter
A HandlerAdapter invokes the controller, running any HandlerInterceptors before and after.
Step 4
Get model and view
The controller returns model data plus a view name, or a body object for REST responses.
Step 5
Resolve and render
A ViewResolver resolves the view and renders the model, or an HttpMessageConverter serializes the body to JSON.
What Interviewer Expects
- Understanding DispatcherServlet as the front controller
- Roles of HandlerMapping, HandlerAdapter, and ViewResolver
- Where HandlerInterceptors fit in the flow
- Difference between view rendering and @ResponseBody serialization
- How Spring Boot auto-configures and maps it
Common Mistakes
- Confusing the DispatcherServlet with the controller itself
- Thinking each controller has its own servlet
- Ignoring HandlerMapping and HandlerAdapter in the flow
- Believing a ViewResolver is used for @RestController JSON responses
- Not knowing exceptions are routed to HandlerExceptionResolvers
Best Answer (HR Friendly)
“The DispatcherServlet is the single front door of a Spring web app: every request comes to it first, and it figures out which controller should handle the request and how to build the response. This central coordination keeps the routing organized and consistent.”
Code Example
@RestController
@RequestMapping("/api/users")
public class UserController {
private final UserService service;
public UserController(UserService service) {
this.service = service;
}
@GetMapping("/{id}")
public UserDto getUser(@PathVariable Long id) {
// DispatcherServlet mapped this request here via HandlerMapping,
// and an HttpMessageConverter serializes the return value to JSON.
return service.findById(id);
}
}Follow-up Questions
- How does the DispatcherServlet differ from a HandlerMapping?
- What is the role of a HandlerAdapter?
- When is a ViewResolver used versus an HttpMessageConverter?
- How do HandlerInterceptors differ from servlet filters?
- How does Spring Boot register and map the DispatcherServlet?
MCQ Practice
1. What design pattern does the DispatcherServlet implement?
The DispatcherServlet is the front controller: a single entry point that routes all requests to the appropriate handlers.
2. Which component maps a request URL to a controller method?
HandlerMapping determines which controller and method should handle a given request URL and method.
3. For an @RestController returning an object, what produces the JSON?
An HttpMessageConverter serializes the returned object to JSON; no ViewResolver is involved for @ResponseBody responses.
Flash Cards
What is the DispatcherServlet? — Spring MVC's front controller servlet that receives all requests and orchestrates handling and the response.
What does HandlerMapping do? — Determines which controller method should handle a given request URL and HTTP method.
What does a HandlerAdapter do? — Invokes the resolved controller method in a uniform way for the DispatcherServlet.
View vs REST response? — Views go through a ViewResolver and render a model; @ResponseBody objects are serialized by an HttpMessageConverter.
How is it configured in Spring Boot? — Auto-configured and mapped to '/' by default, so you rarely declare it manually.
Continue Learning
Related Interview Questions
What is the difference between @Controller and @RestController in Spring?
easy
How does exception handling work in Spring MVC with @ControllerAdvice?
medium
How does Spring Boot embed a web server and how does the embedded Tomcat work?
medium
What is dependency injection in Spring and how does the IoC container work?
medium