Sumanth
Sumanth

Reputation: 179

when @Around is used for Spring AOP then data is not retrieved

I am spring AOP and i have @Around like below

@Around(value = "execution(* com.spring.rest.controller.Controller.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
    long start = System.currentTimeMillis();
    joinPoint.proceed();
    long taken = System.currentTimeMillis() - start;
    logger.info("around this {} time taken is {}", joinPoint, taken);
} 

In my Rest controller, I have getmapping and when I call that routing in the browser there is no data retrieved.

From the logs, I found that it was returning null(below log info) -- @Around is executed and it took 20seconds, @AfterReturning is executed with returning null

2020-05-07 00:41:03.083  INFO 366372 --- [nio-8080-exec-1] lication$$EnhancerBySpringCGLIB$$21c1be2 : around this execution(List com.spring.rest.controller.Controller.getm()) time taken is 20
2020-05-07 00:41:03.084  INFO 366372 --- [nio-8080-exec-1] lication$$EnhancerBySpringCGLIB$$21c1be2 : returning execution(List com.spring.rest.controller.Controller.getm()) returned with value null

but when I remove the @Around, API is working perfectly.

can I know what could be the reason and how to solve this issue?

Upvotes: 1

Views: 374

Answers (1)

Sam
Sam

Reputation: 153

You need to return the object.

public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    long start = System.currentTimeMillis();
    Object object = joinPoint.proceed();
    long taken = System.currentTimeMillis() - start;
    logger.info("around this {} time taken is {}", joinPoint, taken);
    return object;
} 

Upvotes: 3

Related Questions