What happens when an HTTP request reaches a Spring MVC application?
- Servlet container (Tomcat) accepts the connection and assigns a worker thread (a virtual thread if enabled).
- Filter chain runs: servlet filters such as encoding, CORS, the Spring Security
FilterChainProxy(authentication and authorisation), request logging. - DispatcherServlet (the front controller) receives the request.
- HandlerMapping (
RequestMappingHandlerMapping) finds the controller method matching the path, HTTP method, headers and content type. - HandlerInterceptor.preHandle runs for each interceptor (can short-circuit).
- HandlerAdapter invokes the method. Argument resolvers build parameters (
@PathVariable,@RequestParam,@RequestBodythrough anHttpMessageConvertersuch as Jackson,@AuthenticationPrincipal);@Validtriggers Bean Validation. - The controller calls services; the return value is processed by return value handlers. For
@RestController, a message converter serialises it, chosen by content negotiation with theAcceptheader. postHandleandafterCompletioninterceptor callbacks run.- Exceptions go to HandlerExceptionResolvers, including
@ExceptionHandlermethods in@ControllerAdvice. - The response flows back out through the filters.
Filters vs interceptors vs AOP
- Filters: servlet level, see every request (including static resources and errors), can wrap request and response. Use for security, CORS, compression, request IDs.
- Interceptors: Spring MVC level, know which handler was selected. Use for per-controller concerns such as locale or auditing which endpoint ran.
- AOP: any Spring bean method, not HTTP-specific. Use for transactions, metrics and business-level cross-cutting concerns.
Follow-up questions interviewers ask
Try answering each one before you open it.
How does @ControllerAdvice work and how do you scope it?
@RestControllerAdvice beans are registered with ExceptionHandlerExceptionResolver; when a controller throws, Spring finds the most specific @ExceptionHandler method for the exception type, first in the controller itself, then in advice beans in @Order order. Scope advice with basePackages, assignableTypes or annotations so that, for example, an internal admin API and the public API can return different error formats.
How does Spring choose the HttpMessageConverter?
For @RequestBody it uses the request's Content-Type; for responses it intersects the client's Accept header with the media types the available converters can produce for the return type, then picks the first match in converter order. Jackson's converter handles application/json; registering a CBOR, Protobuf or XML converter adds those formats without changing controllers.