我编写了一个简单的AOP来记录请求和执行时间。一切都正常,但是在使用注释记录执行时间时,虽然http状态代码是200,但它没有重新处理任何响应。
你能告诉我有什么问题吗?
控制器:
@LogExecutionTime
@GetMapping("/test")
public String index(){
return "Hello";
}方面:
@Around("@annotation(LogExecutionTime)")
public void logTime(ProceedingJoinPoint jp) throws Throwable{
watch.start() // common lang stopwatch
jp.proceed();
watch.stop();
log,info("Execution time " +watch);
}在日志中,我可以看到它显示了执行时间,但在postman中,我是而不是,如果我评论@LogExecutionTime注释,就会得到响应"hello“--它运行良好
发布于 2022-05-16 11:07:37
类型“”的方面具有修改返回值的能力。proceed方法返回原始方法的值,您正在默默地忽略该方法,并选择不返回任何内容(我认为这将默认为返回null)。您需要按以下方式修改您的方法:
@Around("@annotation(LogExecutionTime)")
public Object logTime(ProceedingJoinPoint jp) throws Throwable{
watch.start(); // common lang stopwatch
Object returnValue = jp.proceed();
watch.stop();
log.info("Execution time " +watch);
return returnValue;
}此外,您还可能希望将watch.stop()调用放在finally块中,以防观察到的方法抛出异常。
https://stackoverflow.com/questions/72258112
复制相似问题