Reputation: 2222
I'm really tired of all this @ControllerAdvice, @ExceptionHandler, DefaultHandlerExceptionResolver, ErrorAttributes, Whitelabel Error Page
stuff.
I want to get rid of it all and handle exceptions manually in the first filter of my filter chain, like this:
try {
chain.doFilter()
} catch (Exception e) {
...
}
Note: I do know how to disable Whitelabel Error Page
:
@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})
public class Application
But that's not the only thing I want to disable. I also want to get rid of DefaultHandlerExceptionResolver
.
Upvotes: 2
Views: 5344
Reputation: 22952
Much/most of the error handling is configured by ErrorMvcAutoConfiguration
. So you will need to exclude this auto-configuration from your application:
Via annotation:
@EnableAutoConfiguration(exclude = org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration)
Or configuration properties:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
If you are using reactive Spring, then you'll want to exclude ErrorWebFluxAutoConfiguration
instead.
Keep in mind there may be other configurations that configure other error beans, but this should take care most of it. You'll need to debug/step through the application start process to find out where/what configures those other beans and disable that auto-configuration as well.
From my debugging, @ControllerAdvice
, @ExceptionHandler
, and DefaultHandlerExceptionResolver
get bootstrapped by WebMvcConfigurationSupport, specifically the handlerExceptionResolver
method.
As stated in the javadocs:
An alternative more advanced option is to extend directly from this class and override methods as necessary, remembering to add @Configuration to the subclass and @Bean to overridden @Bean methods. For more details see the javadoc of @EnableWebMvc.
The advanced option is likely what you want since you want to completely take control over the error handling Spring Boot provides. So following the advice from the javadoc:
@Configuration
public class CustomWebMvcConfigurationSupport extends WebMvcConfigurationSupport {
@Override
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
// Define your custom resolvers here.
// List must NOT be empty otherwise default resolves will kick in.
}
@Bean
@Override
public HandlerExceptionResolver handlerExceptionResolver(
@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager) {
// Or completely take control over the resolvers
HandlerExceptionResolverComposite composite = new HandlerExceptionResolverComposite();
composite.setOrder(0);
composite.setExceptionResolvers(Collections.emptyList());
return composite;
}
}
But again, as stated above, you will need to debug/step through the application start process to find out where/what configures other beans you want to disable and override/disable that configuration.
Upvotes: 4