Spring Boot Disable /error mapping
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot automatically registers /error so unhandled exceptions and status failures produce a default response. That behavior is useful during development, but many production systems need strict, custom error contracts. Disabling or replacing the default mapping is mostly about taking ownership of error flow at the framework boundaries.
How Default Error Mapping Works
By default, Spring Boot configures BasicErrorController through ErrorMvcAutoConfiguration. This controller handles failures that were not fully resolved by other handlers and sends either HTML or JSON depending on content negotiation.
That means even if you have @ControllerAdvice, some cases still end up at /error, especially routing errors and container-level failures. If you need complete control, you typically choose one of two approaches:
- Replace default behavior with your own error controller.
- Disable error MVC auto configuration and handle failures yourself.
The first approach is safer for most teams because it preserves expected framework flow while giving you custom payloads.
Replace /error With a Custom Controller
A practical pattern is to keep the endpoint but return your own contract. This avoids surprises in filters, proxies, and clients that already expect /error to exist.
With this, clients always get predictable JSON instead of default HTML pages.
Disable Whitelabel and Tune Exposure
If your app accidentally returns HTML error pages, disable the whitelabel view and control detail fields.
These settings do not remove /error, but they reduce leakage of internal details.
When you need detailed responses in non-production profiles, use profile-based configuration instead of global settings.
Full Disable of Error MVC Auto Configuration
If you truly want no Boot error controller at all, exclude auto configuration and provide your own handling stack.
Then add explicit exception handlers:
This route gives maximum control but demands more test coverage because framework fallbacks are removed.
Test Error Behavior End to End
After changes, verify several paths:
- missing route
- validation failure
- uncaught exception
Also test with Accept: text/html and Accept: application/json to ensure your app does not unexpectedly switch formats.
Common Pitfalls
- Disabling auto configuration without adding comprehensive global handlers.
- Returning inconsistent payload shapes between validation and runtime errors.
- Leaving stack traces enabled in production responses.
- Assuming
@ControllerAdvicecovers all container-level error cases. - Forgetting content negotiation tests after replacing
/error.
Summary
- Default
/errormapping comes from Boot error MVC auto configuration. - Replacing
/erroris usually safer than removing it completely. - Use properties to disable whitelabel pages and sensitive detail leakage.
- Excluding auto configuration requires a robust custom error strategy.
- Validate error behavior with real HTTP requests and different
Acceptheaders.
Related reading
- Spring boot does not load logback-spring.xml
- Spring boot doesn't load data to initialize database using data.sql
- Spring Boot embedded HornetQ cluster not forwarding messages
- Spring Boot enable http requests logging access logs
- Spring Boot enabling CORS by application.properties
- Spring boot errorjava.lang.ArrayStoreException sun.reflect.annotation.TypeNotPresentExceptionProxy
- Spring Boot extending CrudRepository
- Spring boot external application.properties

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.