Search code examples
spring-bootaop

joinPoint of @Around( AOP ) proceeds but doesn't return result


I use @Around to implement AOP with springboot. Like below

@Around("cut()")
public void advice( ProceedingJoinPoint proceedingJoinPoint ) throws Throwable {
    System.out.println("@Around start");
    proceedingJoinPoint.proceed();
    System.out.println("@Around end");
}

The joinPoint is my controller & The "@Mylog" is my custom annotation.Like below

@MyLog
@RequestMapping("/log")
public String getLog() throws InterruptedException {
    System.out.println("This is joinPoint");
    return "Hello World";
}

When I try to get route "/log" with browser, the information is printed as expected but nothing is returned to browser(I expect "Hello World" will be returned). Like below

@Around start
This is joinPoint
@Around end

Any advice?


Solution

  • It should not surprise you that if your around advice returns nothing (void) instead of the proceed() result, then in fact nothing is returned. ;-) How about this?

    @Around("cut()")
    public Object advice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("@Around start");
        try {
            return proceedingJoinPoint.proceed();
        }
        finally {
            System.out.println("@Around end");
        }
    }