【问题标题】:Global Exception Handling via Spring Advice In a MQ based Spring Boot Application在基于 MQ 的 Spring Boot 应用程序中通过 Spring Advice 处理全局异常
【发布时间】:2018-08-28 22:03:19
【问题描述】:

我有一个 MQ Spring Boot PaaS 应用程序,我需要通过一个通用异常处理程序类 (GlobalExceptionHandler) 实现异常处理。我的 PaaS 应用程序从源队列接收消息,通过 spring jpa 执行一些数据库操作并将响应写回目标队列。

我需要通过 GlobalExceptionHandler 类处理所有的数据库 RuntimeException、自定义业务异常和其他检查异常。

我的 GlobalExceptionHandler 将为每个异常定义处理程序(方法)。在我的处理程序中,我将首先记录异常,然后创建一个 [error code, desc],然后我需要将其返回到主流程。

我的应用程序中没有任何控制器。所以我认为,我不能使用@ControllerAdvice。目前我正在使用 spring AOP @AfterThrowing,如下所示,但我无法从处理程序返回 [code, desc]。

@AfterThrowing(pointcut = "execution(* com.abc.xyz.service..*(..)) ", 
throwing = "dataNotFoundException")
    public void handleDataNotFoundException(DataNotFoundException dataNotFoundException) {
       LOGGER.info("Info : " + dataNotFoundException.getMessage());
       // code, desc need to create here and send it back to calling place.
       // I need to change the return type here from void.
    }

谁能指导我在这里实现异常处理。

【问题讨论】:

    标签: spring spring-boot exception-handling spring-aop


    【解决方案1】:

    正如我解释的here@AfterThrowing 不能修改返回值或以其他方式更改您的应用程序的执行流程。你甚至无法捕捉到那里的异常。您需要改用@Around 建议。

    我建议你先阅读一些文档,然后再提出更多后续问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2022-10-19
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多