Quick revision
The short answer
A HandlerMapping answers which handler should process this request, for example RequestMappingHandlerMapping finds the @RequestMapping method that matches. A HandlerAdapter answers how to call that kind of handler, for example RequestMappingHandlerAdapter resolves the method's arguments and invokes it. They are separate so that DispatcherServlet can support several handler styles, annotated controllers, functional routes, HttpRequestHandler beans and static resources served by ResourceHttpRequestHandler through a SimpleUrlHandlerMapping, without knowing any of them.
What to remember
In this answer, we saw that a HandlerMapping finds the handler and its interceptors for a request, with RequestMappingHandlerMapping matching @RequestMapping methods, and a HandlerAdapter invokes a particular kind of handler, with RequestMappingHandlerAdapter resolving arguments and writing results. They are separate so that DispatcherServlet can support annotated controllers, functional routes, HttpRequestHandler beans and static resources, served by SimpleUrlHandlerMapping and ResourceHttpRequestHandler, without depending on any of them.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A HandlerMapping answers which handler should process this request, for example RequestMappingHandlerMapping finds the @RequestMapping method that matches. A HandlerAdapter answers how to call that kind of handler, for example RequestMappingHandlerAdapter resolves the method's arguments and invokes it. They are separate so that DispatcherServlet can support several handler styles, annotated controllers, functional routes, HttpRequestHandler beans and static resources served by ResourceHttpRequestHandler through a SimpleUrlHandlerMapping, without knowing any of them.
Why this question is asked
These two interfaces are the hinges of Spring MVC. They explain why the same application can mix annotated controllers with functional routes, how static files are served, and where to look when a request is routed to the wrong place. Interviewers ask the question to see whether we understand the design rather than just the annotations. In this answer, we will recap what a handler is, look at what each interface does, see why they are split, walk through the handler styles Spring supports, and see how static resources fit.
1A Quick Recap of Handlers
In Spring MVC, a handler is any object that can process a request. Most of the time it is a method in a @Controller class, but it can also be a function registered with RouterFunctions, a bean implementing HttpRequestHandler, or the built-in handler that serves static files. DispatcherServlet, the single servlet in front of all requests, does not know these types directly. It talks to them through two small interfaces.
2What HandlerMapping Does
A HandlerMapping has one main job: given a request, return the handler for it, together with the interceptors that apply, as a HandlerExecutionChain. It returns null when it has no match, and DispatcherServlet then asks the next mapping, in order.
RequestMappingHandlerMapping is the one we use most. At startup it scans controller beans for @RequestMapping, @GetMapping and the other annotations, and builds a registry from each mapping's conditions, the path pattern, HTTP method, params, headers, consumes and produces, to the method. At request time it finds the best match and returns that method as a HandlerMethod.
3What HandlerAdapter Does
Finding the handler is only half the work. A HandlerMethod cannot simply be called: its parameters need values from the path, query, headers and body, and its return value needs to become a response. A HandlerAdapter knows how to do that for one kind of handler. DispatcherServlet asks each adapter supports(handler), and the first that says yes gets handle(request, response, handler).
RequestMappingHandlerAdapter handles HandlerMethod objects. It uses argument resolvers to fill each parameter, invokes the method, and uses return value handlers and HttpMessageConverters to write the result. That is where all the annotation-driven convenience lives.
4Why They Are Separate
Splitting find from invoke means each new handler style needs only a mapping that can find it and an adapter that can call it, and DispatcherServlet's own code never changes. The mapping cares about URL rules; the adapter cares about calling conventions. A functional route and an annotated method may both match /prices/A-1, but they are called in completely different ways, and the split lets each be handled by the component that understands it.
This is the strategy pattern applied twice, and it is why we can add our own handler style if we ever need one, by registering a HandlerMapping and a HandlerAdapter bean.
| Handler style | HandlerMapping | HandlerAdapter |
|---|---|---|
@RequestMapping methods | RequestMappingHandlerMapping | RequestMappingHandlerAdapter |
Functional routes (RouterFunction) | RouterFunctionMapping | HandlerFunctionAdapter |
HttpRequestHandler beans | SimpleUrlHandlerMapping or BeanNameUrlHandlerMapping | HttpRequestHandlerAdapter |
| Static resources | SimpleUrlHandlerMapping (/**) | HttpRequestHandlerAdapter (ResourceHttpRequestHandler) |
| Legacy Controller interface | BeanNameUrlHandlerMapping | SimpleControllerHandlerAdapter |
5Several Styles in One Application
Here is a functional route living next to the annotated controller above. Both are served by the same DispatcherServlet.
Notice that DispatcherServlet does the same steps for both requests. Only the mapping and adapter that respond differ. The mappings are consulted in order, and annotated controllers and functional routes are asked before the static resource mapping, so an endpoint always wins over a file with the same path.
6How Static Resources Fit
Static files are just another handler style. Spring Boot registers a SimpleUrlHandlerMapping for the /** pattern, with a very low priority so that it is consulted last, pointing to a ResourceHttpRequestHandler. That handler looks for the file in classpath:/static/, /public/, /resources/ and /META-INF/resources/, adds caching headers and serves it. ResourceHttpRequestHandler implements HttpRequestHandler, so HttpRequestHandlerAdapter calls it.
That is why a request for /app.js is served from src/main/resources/static/app.js without any controller, and why a request for an unknown path that no controller handles ends up at the resource handler, which then reports a missing resource as a 404.
7Follow-up questions
Can one request be matched by several HandlerMappings?
Several could match in principle, but DispatcherServlet stops at the first mapping, in order, that returns a handler. That is why the order matters: annotated controllers come before the catch-all resource mapping. If we register our own mapping, we give it an order to place it correctly.
Where do interceptors attach, to the mapping or the adapter?
To the mapping. The HandlerMapping returns a HandlerExecutionChain with the interceptors whose path patterns match, and DispatcherServlet calls them around the adapter's handle call. Interceptors registered through WebMvcConfigurer are added to the mappings Spring configures.
Is there an equivalent in WebFlux?
Yes. DispatcherHandler uses HandlerMapping and HandlerAdapter interfaces from the reactive package, with the same split of finding and invoking.
8Conclusion
In this answer, we saw that a HandlerMapping finds the handler and its interceptors for a request, with RequestMappingHandlerMapping matching @RequestMapping methods, and a HandlerAdapter invokes a particular kind of handler, with RequestMappingHandlerAdapter resolving arguments and writing results. They are separate so that DispatcherServlet can support annotated controllers, functional routes, HttpRequestHandler beans and static resources, served by SimpleUrlHandlerMapping and ResourceHttpRequestHandler, without depending on any of them.
Open with
A HandlerMapping decides which handler should process a request. DispatcherServlet asks the mappings in order, and the first one that matches returns a HandlerExecutionChain, the handler plus its interceptors.
Then, if they want more
The one we use most is RequestMappingHandlerMapping, which builds a registry of all @RequestMapping methods at startup and finds the best match for each request. A HandlerAdapter knows how to invoke a certain type of handler: DispatcherServlet asks each adapter whether it supports the handler, and calls handle on the one that does.
RequestMappingHandlerAdapter resolves method arguments, invokes the method and writes the return value through message converters. They are separate because finding and calling are different concerns, and the split lets DispatcherServlet support many handler styles without knowing any of them.
Annotated controllers use the RequestMapping pair, functional routes use RouterFunctionMapping with HandlerFunctionAdapter, HttpRequestHandler beans use HttpRequestHandlerAdapter, and the old Controller interface has its own adapter. Static resources are just another case: Boot registers a SimpleUrlHandlerMapping for slash star star, consulted last, which points to ResourceHttpRequestHandler serving files from the static folders.
So to add a new style, I would only add a mapping and an adapter, and DispatcherServlet's own flow stays the same.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What do HandlerMapping and HandlerAdapter do, and why are they separate?": https://scrivio-interview-library.netlify.app/spring-boot/mvc-architecture/72df66c9de/