我写了一个简单的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);
}
log中的显示的是执行时间但在postman中我是而不是获取响应"hello"如果我注释@LogExecutionTime注释它会正常工作
字体"具有修改返回值的能力。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
块中,以防观察到的方法抛出异常。