完全禁用 Spring Boot 异常处理? [英] Completely disable Spring Boot exception handling?

查看:78
本文介绍了完全禁用 Spring Boot 异常处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的厌倦了所有这些@ControllerAdvice、@ExceptionHandler、DefaultHandlerExceptionResolver、ErrorAttributes、Whitelabel Error Page 的东西.

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) {
    ...
}

注意:我知道如何禁用Whitelabel Error Page:

@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})
public class Application

但这不是我想要禁用的唯一内容.我也想摆脱 DefaultHandlerExceptionResolver.

But that's not the only thing I want to disable. I also want to get rid of DefaultHandlerExceptionResolver.

推荐答案

大部分/大部分错误处理由 ErrorMvcAutoConfiguration 配置.因此,您需要从应用程序中排除此自动配置:

Much/most of the error handling is configured by ErrorMvcAutoConfiguration. So you will need to exclude this auto-configuration from your application:

通过注释:

@EnableAutoConfiguration(exclude = org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration)

或配置属性:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration

如果您使用的是响应式 Spring,那么您需要排除 ErrorWebFluxAutoConfiguration.

If you are using reactive Spring, then you'll want to exclude ErrorWebFluxAutoConfiguration instead.

请记住,可能还有其他配置会配置其他错误 bean,但这应该注意大部分.您需要调试/逐步完成应用程序启动过程,以找出配置其他 bean 的位置/内容并禁用该自动配置.

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.

在我的调试中,@ControllerAdvice@ExceptionHandlerDefaultHandlerExceptionResolverWebMvcConfigurationSupport,特别是 handlerExceptionResolver 方法.

From my debugging, @ControllerAdvice, @ExceptionHandler, and DefaultHandlerExceptionResolver get bootstrapped by WebMvcConfigurationSupport, specifically the handlerExceptionResolver method.

如 javadoc 中所述:

As stated in the javadocs:

另一种更高级的选项是直接从此类扩展并根据需要覆盖方法,记住将@Configuration 添加到子类和 @Bean 以覆盖 @Bean 方法.更多细节参见@EnableWebMvc 的javadoc.

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.

高级选项可能是您想要的,因为您希望完全控制 Spring Boot 提供的错误处理.因此,请遵循 javadoc 的建议:

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;
    }
}

但同样,如上所述,您将需要调试/逐步执行应用程序启动过程,以找出要禁用和覆盖/禁用该配置的其他 bean 的配置位置/内容.

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.

这篇关于完全禁用 Spring Boot 异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆