✅ What is DispatcherServlet
?
DispatcherServlet
is the Front Controller in the Spring MVC architecture. It is a subclass of HttpServlet
and serves as the entry point for all HTTP requests in a Spring web application.
๐ Responsibilities of DispatcherServlet
It intercepts all incoming HTTP requests and delegates them to the appropriate components for further processing, using annotations such as:
-
@Controller
-
@RequestMapping
-
@GetMapping
,@PostMapping
, etc.
⚙️ Internal Delegation Flow
DispatcherServlet coordinates the entire web request handling by delegating to various components:
1. HandlerMapping
-
Purpose: Determines which
@Controller
and method should handle the request. -
Examples:
-
RequestMappingHandlerMapping
-
BeanNameUrlHandlerMapping
-
2. HandlerAdapter
-
Purpose: Invokes the matched controller method with appropriate arguments.
-
Examples:
-
RequestMappingHandlerAdapter
-
3. ViewResolver
-
Purpose: Resolves the logical view name (like
"home"
) to a physical view (like/WEB-INF/views/home.jsp
). -
Examples:
-
InternalResourceViewResolver
-
ThymeleafViewResolver
-
4. HandlerExceptionResolver
-
Purpose: Handles any exceptions thrown during controller execution and maps them to error views or responses.
-
Examples:
-
DefaultHandlerExceptionResolver
-
ResponseStatusExceptionResolver
-
๐ฆ DispatcherServlet Request Flow with Annotations
✅ DispatcherServlet with Spring Annotations
1. @Controller
or @RestController
Used to define a controller class that handles web requests.
-
@RestController = @Controller + @ResponseBody
2. @RequestMapping
, @GetMapping
, @PostMapping
, etc.
Used to map HTTP requests to handler methods.
3. @ResponseBody
Binds the return value directly to the HTTP response body.
4. @RequestParam
, @PathVariable
, @RequestBody
These help extract data from the request.
5. @ExceptionHandler
Used to handle exceptions in the controller layer.
✅ How DispatcherServlet is Registered (Spring Boot)
In Spring Boot, DispatcherServlet
functions exactly the same as in traditional Spring MVC, but Spring Boot auto-configures and registers it for you, simplifying setup.
๐ง Spring Boot Auto-Configuration
When you create a Spring Boot web application (with spring-boot-starter-web
), Spring Boot:
-
Automatically registers
DispatcherServlet
as a bean. -
Automatically maps it to the root path (
/
) using theDispatcherServletRegistrationBean
. -
Sets up default
HandlerMapping
,HandlerAdapter
,ViewResolver
, andExceptionResolvers
.
๐ Default DispatcherServlet URL Mapping
You can change it:
๐งต Customizing DispatcherServlet in Spring Boot
If needed, you can override the default DispatcherServlet
bean:
To register it manually (optional):
๐ง Properties (optional in application.properties
)
✅ Summary of Key Annotations Involved with DispatcherServlet
Annotation | Purpose |
---|---|
@Controller | Marks a class as a web controller |
@RestController | Same as @Controller but returns response body directly |
@RequestMapping | Maps request URL to method |
@GetMapping , @PostMapping | Specialized shortcuts for specific HTTP methods |
@ResponseBody | Returns data directly to HTTP response |
@RequestParam | Gets query parameters |
@PathVariable | Extracts values from the URL |
@RequestBody | Binds JSON body to method param |
@ExceptionHandler | Handles exceptions at controller level |
๐ DispatcherServlet with Annotations - Simple Spring Boot Example