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?