Video in progress
What path does an HTTP request take to a controller method in Spring MVC?
Tomcat → servlet filters (Security lives here too) → DispatcherServlet → HandlerMapping finds the controller method → interceptors → HandlerAdapter prepares the arguments (reads JSON, validates) → the method → an HttpMessageConverter writes the response.
📖 Answer
Step by step:
- Tomcat accepts the connection and hands the request to a worker thread.
- Filters:
CharacterEncodingFilter, the Spring Security chain, your ownOncePerRequestFilters. DispatcherServlet, the single entry point into Spring MVC.HandlerMapping(RequestMappingHandlerMapping) finds the method by URL, HTTP method andconsumes/produces. If it can't, you get 404; if the method doesn't match, 405.HandlerInterceptor.preHandle, for example for language or audit.HandlerAdapter: theHandlerMethodArgumentResolvers fill in the parameters:@PathVariable,@RequestBody(Jackson),@AuthenticationPrincipal.@Validis checked here too.- The controller method is called.
- The
HandlerMethodReturnValueHandlertakes the returned value; with@ResponseBody, anHttpMessageConverterwrites it as JSON. - If an exception is thrown, a
HandlerExceptionResolverhandles it (@ExceptionHandlerand@ControllerAdvicework here). postHandle/afterCompletion, then the filters in reverse order.
This is very handy when debugging. For example, @ControllerAdvice doesn't fire for a request rejected in a Security filter, because the request never reached the DispatcherServlet.
Sign up to mark studied questions and save your progress.