【问题标题】:How to create global exception handler如何创建全局异常处理程序
【发布时间】:2020-06-19 02:23:59
【问题描述】:

在actframework中,我们可以使用@catch来处理异常,但它只对当前类和父类有效。如果我想创建一个全局异常处理程序,就像SpringBoot 中的@ExceptionHandler,我该怎么办?

【问题讨论】:

    标签: actframework


    【解决方案1】:

    只需将@Global 注释添加到您的@Catch 方法中,例如

    public class AppExceptionHandler {
        @Global
        @Catch(value = Throwable.class, priority = 1)
        public void logThrowable(Throwable throwable) {
            AppEntry.LOGGER.error(throwable, "Global Exception Handler: %s", throwable.getMessage());
        }
    }
    

    使用上面给定的代码,它将捕获在任何请求处理程序中触发的异常,例如,

    【讨论】:

      【解决方案2】:

      您可以使用@ControllerAdvice 在spring boot 中创建全局异常处理程序。下面的答案有示例代码 sn-p 解释相同。

      @ControllerAdvice even by setting the highest precedense for RestControllers not working as it should

      【讨论】:

      • 问题不在于SpringBoot
      • 对不起,我的错。我忽略了它
      猜你喜欢
      • 2010-10-19
      • 2014-04-17
      • 2012-12-31
      • 2011-05-19
      • 2011-08-31
      • 2010-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多