我试图在应用程序中执行控制器方法后打印响应负载,但 afterReturning 方面似乎无法正常工作。它在执行实际方法之前被触发。这是一个 Spring Webflux 应用程序。
@AfterReturning(value="pointCut()",returning = "respValue")
public void logResponse(JoinPoint joinPoint, Object respValue) throws Exception {
.......
log.info("response :: {}",respValue);
}
我的控制器方法:
@PostMapping("/test")
public Flux<Test> addCars(ServerWebExchange serverWebExchange) {
return service.addCars()
.onErrorResume(ex -> {
log.info("error ", ex);
return Mono.error();
});
}
你的pointcut()方法的@PointCut的value属性(@AfterReturning的value属性)是否指向你想要通知的控制器方法?
//method that the value propery of @AfterReturning point to.
@Pointcut(value="the method you want to be advised")
public void pointCut(){}
@AfterReturning(value="pointCut()",returning = "respValue")
public void logResponse(JoinPoint joinPoint, Object respValue) throws Exception {
.......
log.info("response :: {}",respValue);
}
我能够通过修改我的方面来解决此问题,如下所示:
@AfterReturning(value="pointCut()",returning = "respValue")
public void logResponse(JoinPoint joinPoint, Object respValue) throws Exception {
Flux<Object> resp = respValue instanceof Flux ? (Flux<Object>) respValue : Flux.just(respValue);
resp.doOnNext(returnValue ->{
log.info("response :: {}",respValue);
}).subscribe();
}