SpringBoot中@Around实现AOP时切点执行但无返回结果求助
Hey, I totally get what's going on here! The problem lies in how you're handling the return value of your @Around advice method.
Why the browser isn't getting the response
When you use @Around advice, it's responsible for both executing the target method and passing back its return value to the caller. Your current advice method returns void, which means Spring has no idea to pass along the "Hello World" string that your controller method returns. Even though the target method runs correctly, the return value gets swallowed up by your advice.
Fix it by returning the proceed() result
All you need to do is change the return type of your advice method to Object, and return the result of proceedingJoinPoint.proceed(). Here's the corrected code:
@Around("cut()") public Object advice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("@Around start"); // Capture the return value of the target method Object result = proceedingJoinPoint.proceed(); System.out.println("@Around end"); // Return it so the caller (Spring MVC) gets it return result; }
A quick note on @Around behavior
- If your target method returns a value (like your controller's
String), your@Aroundmethod must return that value (or a modified version of it) for the caller to receive it. - Only use a
voidreturn type for@Aroundif the target method itself isvoid—otherwise, you'll lose the return value just like you did here.
After making this change, when you hit the /log endpoint, the browser will correctly receive the "Hello World" response, and your logs will still print in the expected order!
内容的提问来源于stack exchange,提问作者Martin521Wang

