0

controller:

@RestController
@RequestMapping("/test")
public class TestController {

    /**
     * @RequestLog records logs annotation
     */
    @PostMapping("/t1")
    @RequestLog(description = "log test")
    public ApiResult<?> t1(@RequestBody T1VO t1) {
        // FillDataException thrown during execution
        // throw new FillDataException()
    }
}

GlobalExceptionHandler:

@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * If FillDataException is thrown in the controller, this method will be executed
     */
    @ExceptionHandler(FillDataException.class)
    public ApiResult<?> fillDataException(FillDataException e) {
        printExceptionStackTrace(e);
        String defaultMessage = e.getMessage();
        return ApiResult.response(ResultEnum.DATA_INVALID,defaultMessage);
    }
}

aop program that records logs after a client request ends:

@Aspect
@Component
public class LogAspect {

    @Pointcut("@annotation(com.test.common.log.RequestLog)")
    public void logPointCut() {
    }
    
    /**
     * If FillDataException is thrown in the controller, this method will not be executed
     */
    @AfterReturning(value = "logPointCut()",returning = "ret")
    public void logAfter(JoinPoint joinPoint, Object ret) {
        //Processing response results
        if (ret instanceof ApiResult) {
            //logs save to database 
        }
    }

}

After an exception is thrown (handled by controller advice), why does the aop program not execute? How to solve it?

sk w
  • 19
  • 6
  • A controlleradvice isn't implemented by AOP it is part of the contract in Spring MVC. I have also no clue on what you are actually asking as your premise is already wrong. – M. Deinum Sep 26 '22 at 09:15
  • I have an aop program that records logs after a client request ends. But if an exception is thrown (handled by controller advice) during program execution, the aop program that records logs will not execute. How can I solve this problem? – sk w Sep 26 '22 at 09:56
  • Please add code on what you are trying to achieve and what error etc. There is far too little information in your question (and as stated your premise is already wrong). – M. Deinum Sep 26 '22 at 10:00

1 Answers1

1

After an exception is thrown, aop @AfterReturning will not be executed. You can use @AfterThrowing or @Around to handle it.

@AfterThrowing

@AfterThrowing(value = "logPointCut()", throwing = "e")
public void logThrowable(JoinPoint joinPoint, Throwable e) {
    ApiResult<?> apiResult = GlobalExceptionHandler.handleException((Exception) e);
    saveLog(joinPoint,apiResult);
}

@Around

@Around(value = "logPointCut()")
public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
    Exception e = null;
    try {
        return pjp.proceed();
    } catch (Throwable throwable) {
        e = (Exception) throwable;
        throw throwable;
    } finally {
        if (e != null) {
            ApiResult<?> apiResult = GlobalExceptionHandler.handleException(e);
            saveLog(pjp,apiResult);
        }
    }
}
sk w
  • 19
  • 6