Using Spring Boot's ErrorController and Spring's ResponseEntityExceptionHandler correctly
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Error handling is one of those things that separates a polished Spring Boot application from a fragile one. Spring provides two primary mechanisms for managing errors: ErrorController for catching low-level servlet errors and ResponseEntityExceptionHandler for handling exceptions thrown within your controller layer. Understanding when to use each, and how to combine them, gives you full control over the error responses your API returns.
What ErrorController Does
ErrorController is a Spring Boot interface that intercepts requests forwarded to the /error path by the embedded servlet container. This happens for errors that occur outside of your controller methods, such as 404 Not Found responses, filter-chain exceptions, or errors thrown before a controller is even invoked.
Think of ErrorController as the safety net at the very bottom of the stack. If nothing else catches an error, the servlet container forwards the request to /error, and your ErrorController implementation handles it.
Implementing ErrorController
This replaces Spring Boot's default BasicErrorController, giving you a consistent JSON error shape for all servlet-level errors.
What ResponseEntityExceptionHandler Does
ResponseEntityExceptionHandler is a Spring base class designed for use with @ControllerAdvice. It provides default handling for common Spring MVC exceptions like MethodArgumentNotValidException, HttpRequestMethodNotSupportedException, and MissingServletRequestParameterException. You extend it and override methods to customize the response bodies.
This mechanism catches exceptions thrown inside your controllers and any Spring MVC infrastructure exceptions that occur during request processing.
Implementing a ControllerAdvice
By extending ResponseEntityExceptionHandler, you inherit sensible defaults for roughly a dozen Spring MVC exceptions and only override the ones you want to customize.
When to Use Each
Use ResponseEntityExceptionHandler (via @ControllerAdvice) for the vast majority of your error handling. It covers application-level exceptions, validation errors, and Spring MVC infrastructure exceptions. This is where you define custom exception classes, format validation error messages, and return domain-specific error payloads.
Use ErrorController for errors that occur outside the controller dispatch lifecycle. The most common example is a 404 when no handler mapping matches the request at all. Filter-chain errors and errors from the servlet container itself also fall into this category. Without a custom ErrorController, these return Spring Boot's default whitelabel error page or its default JSON structure.
Combining Them Correctly
The best practice is to use both together. Your @ControllerAdvice handles all exceptions within the Spring MVC layer, while your ErrorController catches everything else. The key is to make both produce the same error response structure so that API consumers see a consistent format regardless of where the error originated.
Both your ErrorController and your @ControllerAdvice can delegate to this shared builder, ensuring every error response follows the same contract.
Common Pitfalls
- Implementing only
ErrorControllerand neglecting@ControllerAdvice, which causes Spring MVC exceptions to bypass your custom formatting and fall through to the servlet container. - Forgetting that
@ControllerAdvicedoes not catch errors from servlet filters or interceptors that run before the dispatcher servlet invokes the controller. - Overriding
ResponseEntityExceptionHandlermethods without callingsuper, which silently discards the default handling for exceptions you did not intend to customize. - Registering multiple
@ControllerAdviceclasses without specifying@Order, leading to unpredictable exception handler precedence. - Returning different error response shapes from
ErrorControllerand@ControllerAdvice, forcing API consumers to handle two distinct error formats.
Summary
ErrorControllerhandles servlet-level errors (404s, filter failures) that occur outside the Spring MVC dispatch lifecycle.ResponseEntityExceptionHandlerwith@ControllerAdvicehandles exceptions thrown inside controllers and Spring MVC infrastructure.- Use both together to cover all error scenarios in your application.
- Keep your error response structure consistent across both mechanisms by sharing a common response builder.
- Always specify
@Orderwhen you have multiple@ControllerAdviceclasses to control handler precedence.

