Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

java - 如何使用Spring AOP和WebFlux从joinPoint.proceed()返回的对象(how to get the object returned from joinPoint.proceed() with Spring AOP and WebFlux)

I have a simple aspect (see below) with @Around annotation.

(我有一个简单的方面(见下文),带有@Around注释。)

This aspect works when the the application don't use reactive paradigm.

(当应用程序不使用反应性范式时,此方面适用。)

But when the application returns Mono or Flux doesn't works properly.

(但是,当应用程序返回Mono或Flux时,它将无法正常工作。)

I need to get the object returned from the method to generate a JSON object to use as log, generate events, etc.

(我需要从方法返回的对象生成一个JSON对象以用作日志,生成事件等。)

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;
}

How to get the object returned from joinPoint.proceed() when is Mono or Flux?

(当Mono或Flux时,如何从joinPoint.proceed()返回的对象?)

Thanks in advance.

(提前致谢。)

  ask by Maicon Luz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
                }
            });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...