๐Ÿ”ฅ Launch Offer: Interview Preparation Bundle @ โ‚น2,499 โ‚น9,999 โ€ข 75% OFF. Offer expires in ๐Ÿ”ฅ Launch Offer: Interview Preparation Bundle @ โ‚น2,499 โ‚น9,999 โ€ข 75% OFF. Offer expires in Interview Preparation Bundle @ โ‚น2,499 โ‚น9,999

00:00:00 Get Bundle
home spring boot behind-every-spring-boot-api-dispatcherservlet-handlermapping-explained
Interview guide Spring Boot 33 sections

Behind Every Spring Boot API: DispatcherServlet & HandlerMapping Explained

Imagine you're sitting in a Spring Boot interview and the interviewer asks: "Can you explain how a request reaches the controller in Spring Boot?" Many developers immediately answer:โ€ฆ

Start reading
By javainterviewprep June 9, 2026 6 min read

Imagine you’re sitting in a Spring Boot interview and the interviewer asks:

“Can you explain how a request reaches the controller in Spring Boot?”

Many developers immediately answer:

“DispatcherServlet calls the controller.”

Unfortunately, that answer is incomplete.

The reality is that DispatcherServlet never searches for controllers on its own. Instead, it delegates the responsibility to another important component called HandlerMapping.

Understanding these two components is the key to mastering Spring MVC internals, and it’s one of the most frequently asked interview topics for Java backend developers.

In this article, you’ll learn:

  • What DispatcherServlet is
  • What HandlerMapping does
  • How they work together
  • Internal request flow
  • Startup process
  • Why Spring MVC is so fast
  • Common interview questions
  • Senior-level concepts that impress interviewers

Let’s dive in.


Big Picture

Every request in Spring Boot follows this path:

Browser
โ”‚
โ–ผ
Tomcat
โ”‚
โ–ผ
DispatcherServlet
โ”‚
โ–ผ
HandlerMapping
โ”‚
โ–ผ
HandlerAdapter
โ”‚
โ–ผ
Controller
โ”‚
โ–ผ
Service
โ”‚
โ–ผ
Repository
โ”‚
โ–ผ
Database

The important thing to remember is:

DispatcherServlet does NOT know which controller should execute your request.

Instead, it asks another component:

HandlerMapping


Think of It Like a Company

Instead of using technical terms, imagine a company.

Visitor
โ”‚
โ–ผ
Receptionist
โ”‚
โ–ผ
Employee Directory
โ”‚
โ–ผ
Correct Employee

In Spring MVC:

Real WorldSpring MVC
ReceptionistDispatcherServlet
Employee DirectoryHandlerMapping
EmployeeController

The receptionist never memorizes where every employee sits.

Instead, they look up the office directory.

DispatcherServlet behaves exactly the same way.


What is DispatcherServlet?

Definition

DispatcherServlet is the Front Controller of Spring MVC.

Every HTTP request first reaches this servlet.

It acts as the central coordinator for the entire request lifecycle.

One important fact:

DispatcherServlet does not contain business logic.

Its job is only to coordinate various Spring MVC components.


Real-Life Example

Imagine visiting a hospital.

You walk through the main entrance.

Who greets you?

The receptionist.

The receptionist does not:

  • Treat patients
  • Perform surgery
  • Write prescriptions

Instead, the receptionist:

  • Understands your problem
  • Finds the correct doctor
  • Sends you to the right department

Exactly the same thing happens in Spring Boot.

User

โ†“

DispatcherServlet

โ†“

Find Correct Controller

โ†“

Controller Executes

โ†“

Response Returned

Responsibilities of DispatcherServlet

Whenever a request arrives, DispatcherServlet performs several important tasks.

Receive Request
โ”‚
โ–ผ
Find Controller
โ”‚
โ–ผ
Invoke Controller
โ”‚
โ–ผ
Process Response
โ”‚
โ–ผ
Return HTTP Response

To complete these tasks, it collaborates with several Spring MVC components:

  • HandlerMapping
  • HandlerAdapter
  • ViewResolver
  • ExceptionResolver
  • MessageConverters

You rarely interact with these components directly because DispatcherServlet coordinates everything behind the scenes.


What is HandlerMapping?

Now imagine DispatcherServlet receives the following request:

GET /users

A question immediately arises:

How does DispatcherServlet know that UserController should handle this request?

The answer is simple.

It doesn’t know.

Instead, it asks HandlerMapping.


HandlerMapping Explained

HandlerMapping is essentially a URL lookup mechanism.

Think of it as a giant directory.

URL

โ†“

Controller Method

Example:

URLController Method
/usersUserController.getUsers()
/ordersOrderController.getOrders()
/loginLoginController.login()

Whenever DispatcherServlet receives a request, HandlerMapping immediately tells it which controller method should execute.


Example Controller

@RestController
public class UserController {

@GetMapping("/users")
public String getUsers() {
return "Users";
}
}

When the application starts, Spring scans this class.

Internally it creates a mapping similar to:

/users

โ†“

UserController.getUsers()

This mapping is stored in memory.

Later, when someone requests:

GET /users

HandlerMapping instantly returns:

UserController.getUsers()

Complete Internal Flow

Let’s see the entire journey.

User enters:

http://localhost:8080/users

Step 1

Browser sends request.

โ†“

Step 2

Tomcat receives it.

โ†“

Step 3

Tomcat forwards it to DispatcherServlet.

โ†“

Step 4

DispatcherServlet asks:

“Who handles /users?”

โ†“

Step 5

HandlerMapping searches its stored mappings.

โ†“

Step 6

It finds:

/users

โ†“

UserController.getUsers()

โ†“

Step 7

DispatcherServlet passes the handler to HandlerAdapter, which invokes the controller method.

โ†“

Step 8

Controller executes the business logic.

โ†“

Step 9

The response travels back through DispatcherServlet.

โ†“

Step 10

Tomcat sends the HTTP response to the browser.


How Does HandlerMapping Know All URLs?

This is where Spring Boot becomes incredibly smart.

During application startup:

Spring scans every class.

Whenever it finds:

@RestController

or

@Controller

it inspects methods annotated with:

  • @RequestMapping
  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping

For each mapping, Spring creates an internal lookup.

Conceptually:

/users
โ†“
UserController.getUsers()

-------------------

/orders
โ†“
OrderController.getOrders()

-------------------

/login
โ†“
LoginController.login()

Although the internal implementation is more sophisticated than a simple HashMap, thinking of it as a fast lookup table is a useful mental model.

Because these mappings are prepared before the first request arrives, request routing at runtime is extremely fast.


What Happens If No URL Matches?

Suppose the user requests:

GET /abc

HandlerMapping searches its mappings.

No controller is found.

It informs DispatcherServlet that there is no matching handler.

DispatcherServlet responds with:

404 Not Found

Does DispatcherServlet Search Every Controller?

No.

Searching every controller for every request would be inefficient.

Instead, Spring performs the heavy work once during application startup by scanning annotations and building the mappings in advance.

At runtime, DispatcherServlet simply asks HandlerMapping to retrieve the correct handler, making request processing fast and scalable.


Senior-Level Understanding

Many developers believe DispatcherServlet directly calls controller methods.

That is not true.

There is another important layer:

Browser
โ”‚
โ–ผ
Tomcat
โ”‚
โ–ผ
DispatcherServlet
โ”‚
โ–ผ
HandlerMapping
โ”‚
โ–ผ
HandlerExecutionChain
โ”‚
โ–ผ
HandlerAdapter
โ”‚
โ–ผ
Controller Method

HandlerExecutionChain

Bundles together:

  • Controller
  • Interceptors

Interceptors execute:

  • Before controller
  • After controller
  • After completion

HandlerAdapter

HandlerAdapter knows how to invoke different kinds of handlers.

For annotated controllers, it:

  • Resolves @RequestParam
  • Resolves @PathVariable
  • Converts @RequestBody
  • Performs data binding
  • Invokes the controller method
  • Returns the result to DispatcherServlet

This abstraction allows Spring MVC to support multiple handler types without requiring DispatcherServlet to understand their implementation details.


Common Interview Question

Question

How do DispatcherServlet and HandlerMapping work together?

Answer

DispatcherServlet is the front controller of Spring MVC and receives every incoming HTTP request. Instead of searching for controllers itself, it delegates that responsibility to HandlerMapping. During application startup, Spring scans controller classes and builds mappings between request URLs, HTTP methods, and controller methods. When a request arrives, DispatcherServlet asks HandlerMapping to find the appropriate handler. HandlerMapping returns the matching handler (along with any interceptors in a HandlerExecutionChain), and DispatcherServlet uses a HandlerAdapter to invoke the controller method. Finally, DispatcherServlet processes the result and sends the HTTP response back to the client.


One-Line Interview Summary

DispatcherServlet

The central front controller that receives every HTTP request and coordinates the complete request-processing workflow in Spring MVC.

HandlerMapping

The Spring MVC component responsible for mapping an incoming HTTP request (URL and HTTP method) to the appropriate controller method.


Key Takeaways

  • โœ… Every request first reaches DispatcherServlet.
  • โœ… DispatcherServlet is a coordinator, not a business logic processor.
  • โœ… HandlerMapping maintains URL-to-controller mappings.
  • โœ… These mappings are created during application startup.
  • โœ… DispatcherServlet delegates handler lookup to HandlerMapping.
  • โœ… HandlerAdapter is responsible for invoking the controller method.
  • โœ… This layered architecture makes Spring MVC highly extensible, maintainable, and performant.

๐Ÿ’ก Pro Tip for Interviews: A common follow-up question is, “How does Spring know which controller method to execute before the first request even arrives?” Mention that Spring scans controller classes during startup, reads mapping annotations like @GetMapping and @RequestMapping, and builds the URL-to-handler mappings in advance. This demonstrates a deeper understanding of Spring MVC internals and often distinguishes senior-level candidates from beginners.