如何使用 Spring AOP 和 WebFlux 获取从 joinPoint.proceed() 返回的对象 [英] how to get the object returned from joinPoint.proceed() with Spring AOP and WebFlux
问题描述
我有一个带有 @Around
注释的简单方面(见下文).当应用程序不使用反应式范式时,这方面起作用.但是当应用程序返回 Mono 或 Flux 时无法正常工作.
I have a simple aspect (see below) with @Around
annotation. This aspect works when the the application don't use reactive paradigm. But when the application returns Mono or Flux doesn't works properly.
我需要获取方法返回的对象来生成一个JSON对象来用作日志、生成事件等
I need to get the object returned from the method to generate a JSON object to use as log, generate events, etc.
这是我在非响应类中工作的代码:
Here is my code that works in a non reactive classes:
@Around("@annotation(simpleEvent)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint, SimpleEvent simpleEvent) throws Throwable {
final long start = System.currentTimeMillis();
Object proceed = null;
try {
proceed = joinPoint.proceed();
// here in the real life the object that transformed in json to do others actions
System.out.println(proceed);
final long executionTime = System.currentTimeMillis() - start;
System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
return proceed;
} catch (Exception e) {
e.printStackTrace();
}
return proceed;
}
joinPoint.proceed()
是 Mono 还是 Flux 时如何获取返回的对象?
How to get the object returned from joinPoint.proceed()
when is Mono or Flux?
提前致谢.
推荐答案
你可以这样做,继续返回 Mono 时一样
you can do like this, same when proceed return a Mono
@Around("somePointCut()")
public Object aspectForSomething(ProceedingJoinPoint point) throws Throwable {
Flux flux = (Flux) point.proceed();
return flux
.doOnNext(obj -> {
log.error("just take a look: " + obj);
})
.map(obj -> {
if (obj instanceof SomeBo) {
SomeBo bo = (SomeBo) obj;
bo.setName("do some modification");
return bo;
} else {
return obj;
}
});
}
这篇关于如何使用 Spring AOP 和 WebFlux 获取从 joinPoint.proceed() 返回的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!