Quick revision
The short answer
doDispatch first asks the HandlerMappings for a HandlerExecutionChain, the handler method plus its interceptors, then finds a HandlerAdapter that supports it, RequestMappingHandlerAdapter for @RequestMapping methods. It calls the interceptors' preHandle, lets the adapter resolve the arguments and invoke the method, and has a return value handler write the result, through an HttpMessageConverter for @ResponseBody. Then it calls postHandle, handles any exception with the HandlerExceptionResolvers, and always calls afterCompletion at the end.
What to remember
In this answer, we followed a request to a @RestController method through doDispatch. It gets a HandlerExecutionChain from the handler mappings, finds RequestMappingHandlerAdapter, runs preHandle, lets the adapter resolve arguments, invoke the method and write the return value through an HttpMessageConverter, then runs postHandle, sends any exception to the HandlerExceptionResolvers and always finishes with afterCompletion. We also saw why postHandle cannot change a JSON body.
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.
doDispatch first asks the HandlerMappings for a HandlerExecutionChain, the handler method plus its interceptors, then finds a HandlerAdapter that supports it, RequestMappingHandlerAdapter for @RequestMapping methods. It calls the interceptors' preHandle, lets the adapter resolve the arguments and invoke the method, and has a return value handler write the result, through an HttpMessageConverter for @ResponseBody. Then it calls postHandle, handles any exception with the HandlerExceptionResolvers, and always calls afterCompletion at the end.
Why this question is asked
Most Spring MVC debugging questions end up inside doDispatch: why an interceptor cannot change a JSON body, why an exception handler did not run, why a request returned 406. Knowing its steps in order turns these into quick answers. Interviewers ask for the walk-through because it shows we understand the machinery behind a one-line @GetMapping. In this answer, we will follow a request to a @RestController method through each step of doDispatch, see what each component does and why it exists, and note the details that commonly surprise people.
1A Quick Recap of the Players
DispatcherServlet is Spring MVC's front controller: one servlet that receives every request. Its doDispatch method coordinates a few kinds of strategy beans. A HandlerMapping finds the handler for a request; for annotated controllers that is RequestMappingHandlerMapping. A HandlerInterceptor is code that runs before and after a handler, for example to log timing. A HandlerAdapter knows how to call a certain kind of handler. Argument resolvers and return value handlers convert between HTTP and our method's parameters and return value, and HttpMessageConverters turn bodies into objects and back, such as JSON with Jackson.
We will follow this controller through the whole path.
2Finding the Handler Execution Chain
doDispatch first checks whether the request is multipart and wraps it if so. Then it calls getHandler, which asks each HandlerMapping in order until one returns a match. RequestMappingHandlerMapping matches GET /orders/42 against its registry of mappings, picks the best match, OrderController.get, and binds the path variable id = 42 as a request attribute.
It returns a HandlerExecutionChain: the handler, wrapped as a HandlerMethod, plus the list of interceptors that apply to this path. If no mapping matches, doDispatch calls noHandlerFound, and the request ends as a 404; in recent versions a missing static resource shows up as NoResourceFoundException, which is handled like any other exception.
3Finding the Handler Adapter
DispatcherServlet does not know how to call a HandlerMethod itself. It asks each HandlerAdapter whether it supports this handler, and RequestMappingHandlerAdapter says yes for HandlerMethod objects. This separation is why the same servlet can also serve functional routes or HttpRequestHandler beans, each with its own adapter.
For GET and HEAD requests, doDispatch also gives the handler a chance to answer with 304 Not Modified when the client's cached copy is still fresh, before doing any real work.
4PreHandle and Invoking the Method
Next comes applyPreHandle, which calls preHandle on every interceptor in order. If one returns false, for example a rate limiter that has already written a 429 response, processing stops there, and afterCompletion is called for the interceptors that already ran.
Then doDispatch calls ha.handle. RequestMappingHandlerAdapter builds a ServletInvocableHandlerMethod and resolves each parameter with an argument resolver: PathVariableMethodArgumentResolver reads the bound id and converts "42" to the long 42, and for an @RequestBody parameter RequestResponseBodyMethodProcessor would read the body with a message converter and run validation if @Valid is present. With all arguments ready, it invokes our method through reflection.
11 messages in order. Play them, or step through them one at a time.
5Writing the Return Value
Our method returns an OrderDto. A return value handler decides what to do with it. Because @RestController implies @ResponseBody, RequestResponseBodyMethodProcessor handles it: it looks at the Accept header and the converters available, picks the Jackson JSON converter, and writes the object straight into the response body. Spring Framework 7 adds converters for Jackson 3, so check your version for the converter class name. If no converter can produce a type the client accepts, the result is 406 Not Acceptable.
Notice what this means for timing. With @ResponseBody, the response is written during handle, before postHandle runs. So the adapter returns no ModelAndView, and doDispatch has no view to render.
6PostHandle, Exceptions and afterCompletion
After handle returns, doDispatch calls applyPostHandle, which calls postHandle on the interceptors in reverse order. For a view-based controller, postHandle can still add model attributes; for a REST endpoint the body has already been sent, so changing it in postHandle has no effect. That is a common surprise, and the fix is a ResponseBodyAdvice, which runs before the body is written.
If anything threw an exception, in the interceptors, the argument resolution or our method, doDispatch catches it and processDispatchResult passes it to the HandlerExceptionResolvers in order. ExceptionHandlerExceptionResolver calls a matching @ExceptionHandler method, in the controller or a @ControllerAdvice; ResponseStatusExceptionResolver handles @ResponseStatus and ResponseStatusException; DefaultHandlerExceptionResolver maps Spring's own exceptions, such as a type mismatch, to 400 or 405.
Finally, in a finally block, triggerAfterCompletion calls afterCompletion on every interceptor whose preHandle succeeded, whether the request succeeded or failed. That makes it the right place to stop timers or clean up thread-local state.
- getHandler
HandlerExecutionChainor 404 - getHandlerAdapter
RequestMappingHandlerAdapter - applyPreHandleany false stops the request
- ha.handleresolve args, invoke, write
@ResponseBody - applyPostHandlereverse order; body already written
- processDispatchResultexception resolvers or view rendering
- triggerAfterCompletionalways, in finally
7 steps in order. Play them, or step through them one at a time.
7Follow-up questions
Why can't an interceptor's postHandle modify a JSON response?
Because for @ResponseBody methods, the return value handler writes the body to the response during the adapter's handle call, and the response may already be committed by the time postHandle runs. To change the body, we use a ResponseBodyAdvice bean, which runs just before the converter writes, or a filter that wraps the response if we really need to post-process bytes.
Where does validation of @Valid @RequestBody happen?
During argument resolution, inside the adapter. RequestResponseBodyMethodProcessor reads the body, validates it, and throws MethodArgumentNotValidException on failure, which the exception resolvers turn into a 400 response.
Do servlet filters run inside doDispatch?
No. Filters run before the servlet is called, so Spring Security has already authenticated the request by the time doDispatch starts.
8Conclusion
In this answer, we followed a request to a @RestController method through doDispatch. It gets a HandlerExecutionChain from the handler mappings, finds RequestMappingHandlerAdapter, runs preHandle, lets the adapter resolve arguments, invoke the method and write the return value through an HttpMessageConverter, then runs postHandle, sends any exception to the HandlerExceptionResolvers and always finishes with afterCompletion. We also saw why postHandle cannot change a JSON body.
Open with
doDispatch is the coordinator in DispatcherServlet. For a request like GET /orders/42 to a @RestController, it first checks for multipart, then asks the HandlerMappings for a handler; RequestMappingHandlerMapping finds the best matching method and returns a HandlerExecutionChain, which is the HandlerMethod plus the interceptors for that path.
Then, if they want more
If nothing matches, it is a 404. Next it finds a HandlerAdapter that supports the handler, RequestMappingHandlerAdapter for annotated methods. It calls preHandle on each interceptor, and if one returns false, processing stops.
Then the adapter resolves each argument with argument resolvers, for example converting the path variable to a long or reading and validating an @RequestBody with a message converter, and invokes the method.
The return value goes to a return value handler; for @ResponseBody, it picks an HttpMessageConverter based on the Accept header, typically Jackson, and writes JSON directly, or returns 406 if nothing fits. After that it calls postHandle in reverse order, which is why postHandle cannot change a REST body; ResponseBodyAdvice is the tool for that.
Any exception goes to the HandlerExceptionResolvers, such as the one that calls @ExceptionHandler methods in a @ControllerAdvice. And in a finally block it calls afterCompletion on every interceptor that passed preHandle.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 7
- 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 "Walk through what DispatcherServlet.doDispatch does for a request to a @RestController...": https://scrivio-interview-library.netlify.app/spring-boot/mvc-architecture/ee8dad623b/