Spring MVC

How does Spring Boot route HTTP requests to controllers?

Spring MVC uses DispatcherServlet as the central front controller. It receives HTTP requests, asks HandlerMapping which controller method should handle them, uses HandlerAdapter to invoke that method, and then writes the response.

Spring BootSpring MVCDispatcherServletRequest RoutingBackend

The Short Answer

Spring MVC routes requests through DispatcherServlet. It acts as the central front controller for the web layer.

When an HTTP request comes in, DispatcherServlet does not magically know which controller to call. It asks Spring MVC infrastructure components to find and invoke the correct handler.

The key idea: DispatcherServlet receives the request, HandlerMapping finds the controller method, and HandlerAdapter invokes it.

The Real Problem It Solves

In a web application, many URLs may map to many controller methods.

java
@RestController
@RequestMapping("/api/products")
public class ProductController {

    @GetMapping("/{id}")
    public ProductResponse getProduct(@PathVariable Long id) {
        return productService.getProduct(id);
    }
}

The important question is: when a request arrives for GET /api/products/42, how does Spring find this exact method?

DispatcherServlet coordinates that lookup and invocation process.

The Mental Model

HTTP Request
DispatcherServlet
HandlerMapping
Controller Method
HTTP Response

DispatcherServlet is not usually where your business logic lives. It is the coordinator that routes the request through the Spring MVC machinery.

Step 1: Request Enters the Web Server

A request first reaches the embedded server, usually Tomcat in a typical Spring Boot web application.

Tomcat then passes the request into the servlet layer, where DispatcherServlet is registered to handle web requests.

In Spring Boot, you usually do not manually register DispatcherServlet. Boot auto-configures it for you when Spring MVC is on the classpath.

Step 2: DispatcherServlet Receives the Request

DispatcherServlet is the central entry point for Spring MVC request handling.

It receives the HTTP request and starts asking the MVC infrastructure:

java
Who should handle this request?
How should that handler be invoked?
How should the result be converted into an HTTP response?

Step 3: HandlerMapping Finds the Handler

HandlerMapping is responsible for finding the matching handler for a request.

For annotation-based controllers, this usually means matching:

  • HTTP method, such as GET or POST
  • URL path, such as /api/products/42
  • controller-level mappings
  • method-level mappings
  • path variables and request conditions

Incoming Request

GET /api/products/42

Matched Handler

ProductController.getProduct(id)

Step 4: HandlerAdapter Invokes the Controller Method

Finding the method is not enough. Spring still needs to invoke it correctly.

That is where HandlerAdapter comes in.

The adapter knows how to call the selected handler method, including resolving arguments like:

java
@PathVariable
@RequestParam
@RequestBody
@RequestHeader

This is why your controller method can declare useful parameters and Spring can populate them automatically.

Step 5: Controller Returns a Result

The controller method runs and returns something.

java
@GetMapping("/{id}")
public ProductResponse getProduct(@PathVariable Long id) {
    return productService.getProduct(id);
}

In a REST API, the return value is usually converted to JSON and written into the HTTP response.

In a traditional MVC application, the return value may instead refer to a view name that gets resolved by a ViewResolver.

Step 6: Response Is Written Back

For REST APIs, Spring uses message converters to turn Java objects into response bodies.

java
ProductResponse object
JSON response body
HTTP response

That is why returning a plain Java object from a REST controller can produce JSON automatically.

Where Exceptions Fit

If something goes wrong, DispatcherServlet can delegate exception handling to Spring MVC exception resolvers.

java
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ProductNotFoundException.class)
    public ResponseEntity<ApiError> handleNotFound(...) {
        return ResponseEntity.status(404).body(...);
    }
}

This is how exceptions from controllers or services can become clean HTTP responses.

DispatcherServlet vs Controller

DispatcherServlet

Framework coordinator. Receives requests, finds handlers, invokes MVC infrastructure, and manages the overall flow.

Controller

Your application code. Handles a specific endpoint and delegates business work to services.
DispatcherServlet is framework plumbing. Controllers are your endpoint logic.

The Interview-Friendly Explanation

In Spring MVC, DispatcherServlet acts as the front controller. It receives incoming HTTP requests, uses HandlerMapping to find the matching controller method, uses HandlerAdapter to invoke that method with resolved arguments, handles exceptions if needed, and writes the result back as an HTTP response.

Common Interview Follow-Ups

What is DispatcherServlet?

DispatcherServlet is the central front controller in Spring MVC. It receives web requests and coordinates routing, handler invocation, exception handling, and response rendering.

What does HandlerMapping do?

HandlerMapping finds which handler or controller method should handle a request based on the URL, HTTP method, and mapping annotations.

What does HandlerAdapter do?

HandlerAdapter knows how to invoke the selected handler. For annotation-based controllers, it helps call the controller method and resolve method arguments.

Is DispatcherServlet created manually in Spring Boot?

Usually no. Spring Boot auto-configures DispatcherServlet when the web MVC infrastructure is present.

Is DispatcherServlet the same as a servlet filter?

No. Filters run before requests reach DispatcherServlet. DispatcherServlet is the Spring MVC entry point that routes requests to controllers.

Final Takeaway

Spring MVC request routing is not just “URL calls controller.” DispatcherServlet coordinates the routing machinery that finds the right controller method, invokes it correctly, and turns the result into an HTTP response.