HandlerMapping in Spring MVC — URL to Controller Method Resolution
Fast recall
Quick revision
Core answer
HandlerMapping is the Spring MVC strategy that finds a handler for an incoming request. For annotated controllers, RequestMappingHandlerMapping registers @RequestMapping, @GetMapping, and similar methods at startup, then matches requests by their mapping conditions and returns the handler together with applicable interceptors.Depth of Spring MVC internal knowledge beyond just writing @GetMapping annotations
Not knowing HandlerMapping exists — thinking Spring magically routes requests without understanding the lookup mechanism
Mention multiple HandlerMapping beans can exist with different priorities, and Spring tries them in order until one returns a handler
Interview-ready guide
Interview answer
HandlerMapping is the routing strategy used by Spring MVC's DispatcherServlet. For annotated controllers, RequestMappingHandlerMapping detects methods marked with @RequestMapping, @GetMapping, and related annotations during startup. It registers the complete conditions for each method, including path, HTTP method, parameters, headers, and media types.
Suppose a controller declares @GetMapping("/users/{id}"). When GET /users/42 arrives, DispatcherServlet checks its ordered HandlerMapping objects. The matching mapping returns a HandlerExecutionChain containing that controller method and any interceptors. A HandlerAdapter then resolves arguments such as id and invokes the method.
A missing match normally ends as 404 before controller code runs. Two methods with the same effective conditions cannot both be registered, so startup fails with an ambiguous-mapping error. The names {id} and {name} alone do not make two path patterns different.
HandlerMapping in Spring MVC — URL to Controller Method Resolution
Direct Answer
HandlerMapping is the Spring MVC strategy that finds a handler for an incoming request. For annotated controllers, RequestMappingHandlerMapping registers @RequestMapping, @GetMapping, and similar methods at startup, then matches requests by their mapping conditions and returns the handler together with applicable interceptors.
Code Example
@RestController
@RequestMapping("/api/products")
public class ProductController {
@GetMapping("/{id}")
public Product getProduct(@PathVariable Long id) {
// RequestMappingHandlerMapping mapped GET /api/products/123 to this method
return new Product(id, "Gaming Mouse", 59.99);
}
@PostMapping
public Product createProduct(@RequestBody Product product) {
// POST /api/products mapped here by HTTP method
return product;
}
}
record Product(Long id, String name, Double price) {}
// During startup, RequestMappingHandlerMapping scans this controller:
// Map entry: GET /api/products/{id} -> ProductController.getProduct(Long)
// Map entry: POST /api/products -> ProductController.createProduct(Product)
//
// At runtime, when DispatcherServlet receives GET /api/products/5:
// HandlerMapping.getHandler(request) returns HandlerExecutionChain pointing to getProduct
Common Mistakes & Pitfalls
- Not knowing HandlerMapping exists: Learners think Spring "just knows" which method to call. They don't realize DispatcherServlet delegates to HandlerMapping for this lookup. When routing breaks, they don't know where to look.
- Assuming only one HandlerMapping: Spring registers several by default — RequestMappingHandlerMapping for controllers, SimpleUrlHandlerMapping for static resources. If you register a custom one without setting order, it might not run.
- Conflicting mappings without realizing: Writing two methods with @GetMapping("/users/{id}") and @GetMapping("/users/{name}") causes AmbiguousMappingException at startup because HandlerMapping can't distinguish them — Learners think "oh, one's an id, one's a name" but HandlerMapping sees identical patterns.
Check your understanding
Answer these without looking back at the article.
- What is a HandlerInterceptor in Spring MVC?
- What happens if two controller methods are mapped to the same URL?
- How does Spring Boot auto-configure HandlerMapping?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.