Spring / Spring7 Intermediate to Advanced Interview questions
What is the difference between @ControllerAdvice and a Filter for handling errors in Spring MVC?
@ControllerAdvice paired with @ExceptionHandler operates inside the Spring MVC dispatch layer - it only catches exceptions thrown after HandlerMapping has already matched a controller and the request is being processed by, or on the way into, that controller's code (including argument resolution and interceptor logic). It works with typed model objects and has full access to Spring's HttpMessageConverters, so returning a structured JSON error body with the correct status code is straightforward.
| Aspect | @ControllerAdvice | Filter |
| Runs | Inside MVC dispatch | At the servlet container level, before DispatcherServlet |
| Catches | Exceptions from controller code | Anything, including requests that never reach a controller |
| Works with | Typed model objects, message converters | Raw HttpServletRequest/Response |
A Filter runs earlier and more broadly, wrapping DispatcherServlet itself, so it can intercept problems that never reach a controller at all - a malformed request a security filter rejects, or an exception thrown by another filter. In practice, the two are complementary: use @ControllerAdvice to translate business and validation exceptions into consistent structured error responses, and use filters for cross-cutting infrastructure concerns - authentication failures, request logging, global CORS handling - that need to run whether or not a controller handler was ever reached.
More Related questions...