Spring Boot has become the de facto standard for building modern web applications in the Java ecosystem. It abstracts much of the boilerplate and wiring, but under the hood, it follows a well-orchestrated request handling lifecycle that every backend engineer should understand.
In this blog, weβll walk through the entire journey of an HTTP request in a Spring Boot application β from the moment it hits the server, through the controller and service layers, and back as a response.
Weβll also take a deep dive into the DispatcherServlet, the unsung hero orchestrating the flow.
π Table of Contents
- Introduction
- Request Lifecycle Overview
- Step-by-Step Request Journey
- Understanding DispatcherServlet
- Optional Components
- Summary Flow Diagram
- Conclusion
π§ Introduction
Every time a client (browser, mobile app, Postman, etc.) sends a request to your Spring Boot API, a lot happens behind the scenes. This blog demystifies that process by following the entire journey β helping you understand where you can hook in validations, exception handling, business logic, transactions, and more.
π Request Lifecycle Overview
At a high level, the request journey looks like this:
Client β Embedded Server β DispatcherServlet β Handler Mapping
β Controller β Service β Repository β DB
β Response back through DispatcherServlet β Client
But letβs break it down into precise, technical steps.
π οΈ Step-by-Step Request Journey
πΉ 1. Client Sends HTTP Request
A client sends an HTTP request to the server:
GET http://localhost:8080/api/users/1
This request hits the port where Spring Boot is listening.
πΉ 2. Embedded Server Receives the Request
Spring Boot applications run with an embedded server (default: Tomcat). It listens for incoming HTTP requests.
The embedded server handles:
- Socket communication
- HTTP parsing
- Delegating the request to the Spring context
πΉ 3. DispatcherServlet Takes Over
Once the HTTP request is received, Springβs DispatcherServlet takes control.
π§ What is DispatcherServlet?
DispatcherServlet is Springβs Front Controller β a core component of Spring MVC.
Responsibilities:
- Routing incoming requests to appropriate controllers
- Delegating to other components like view resolvers, handler mappings, converters, exception handlers
- Acting as the coordinator between different parts of the MVC stack
Itβs initialized automatically in Spring Boot via @SpringBootApplication which enables auto-configuration.
πΉ 4. HandlerMapping Resolves the Target Controller
DispatcherServlet consults a HandlerMapping to determine which controller method should handle the request.
- Uses URL, HTTP method (GET, POST), etc.
- Mapped using annotations like
@GetMapping,@RequestMapping
@GetMapping("/api/users/{id}")
public ResponseEntity<UserDTO> getUser(@PathVariable Long id) {
...
}
πΉ 5. HandlerAdapter Invokes the Method
Once the correct handler (method) is found, the HandlerAdapter calls it.
- Converts request parameters, headers, and body to method arguments using
HandlerMethodArgumentResolver. - Handles custom converters,
@RequestParam,@RequestBody, etc.
πΉ 6. Controller Logic is Executed
The controller method executes the request handling logic.
This typically involves calling:
- Business logic layer (
@Service) - Validation
- Response building
πΉ 7. Service Layer Executes Business Logic
The controller calls into a @Service bean.
This is where:
- Domain logic lives
- External service calls happen
- Data transformation and orchestration occurs
πΉ 8. Repository Layer Interacts with Database
The service invokes a DAO/repository component (annotated with @Repository) to fetch or persist data.
Usually powered by:
- Spring Data JPA
- Hibernate
- JDBC Template
Example:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
πΉ 9. Transaction Management (Optional)
If a method is annotated with @Transactional, Spring wraps it with a proxy to manage:
- Begin transaction
- Commit or rollback based on exceptions
This is handled via PlatformTransactionManager behind the scenes.
πΉ 10. Response is Returned to DispatcherServlet
The return value from the controller method (often a POJO or ResponseEntity) is:
- Serialized to JSON using
HttpMessageConverters(e.g., Jackson) - Packaged as a proper HTTP response
πΉ 11. Filters / Interceptors / Advices are Applied
Before and after controller execution:
- Servlet Filters (e.g., for logging, security headers)
- Spring Interceptors (e.g., auth checks, metrics)
-
Exception Handlers (
@ControllerAdvice) -
Response Body Advice (
ResponseBodyAdvice)
These add power and flexibility to your architecture.
πΉ 12. Final HTTP Response is Sent to Client
The serialized response is sent back to the embedded server, which pushes it over the socket to the client.
Example:
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 1,
"name": "John Doe"
}
π§ Deep Dive: DispatcherServlet
Letβs understand DispatcherServlet more deeply.
β Why is it Called a Front Controller?
Because it centralizes request processing in one place before dispatching to handlers.
π Key Responsibilities
| Function | Description |
|---|---|
| Request dispatching | Routes requests to correct controllers |
| Handler resolution | Uses HandlerMapping
|
| Argument resolution | Uses HandlerMethodArgumentResolver
|
| View resolution | Uses ViewResolver (for traditional MVC) |
| Exception handling | Integrates with @ControllerAdvice
|
| Response transformation | Uses HttpMessageConverters
|
π Initialization
Spring Boot auto-registers the DispatcherServlet using DispatcherServletAutoConfiguration. It is mapped to / by default, meaning it handles all incoming requests.
π§© Optional Components
| Component | Purpose |
|---|---|
Filter |
Servlet-level request/response pre-processing |
HandlerInterceptor |
Spring-level pre/post-processing |
@ControllerAdvice |
Centralized exception handling |
@Transactional |
Transaction boundary management |
@Valid |
Request validation |
AOP |
Logging, metrics, cross-cutting concerns |
πΊοΈ Summary Flow Diagram
+------------+ +------------------+ +------------------+
| Client +-----> | Embedded Server +----> | DispatcherServlet|
+------------+ +------------------+ +--------+---------+
|
v
+---------------------+
| HandlerMapping |
+---------------------+
|
v
+---------------------+
| HandlerAdapter |
+---------------------+
|
v
+---------------------+
| Controller |
+---------------------+
|
v
+---------------------+
| Service Layer |
+---------------------+
|
v
+---------------------+
| Repository / DB |
+---------------------+
|
v
+---------------------+
| Response Conversion|
+---------------------+
|
v
+---------------------+
| DispatcherServlet |
+---------------------+
|
v
+-----------+
| Client |
+-----------+
π Conclusion
Understanding the Spring Boot request lifecycle β especially the role of DispatcherServlet β is vital for building clean, scalable, and maintainable applications. From request reception to controller execution to response dispatch, each layer provides powerful hooks for customization, validation, and extensibility.
Next time you debug or architect a Spring Boot API, remember this full journey β and leverage its power to write clean, performant systems.
Top comments (0)