The interceptor is not suitable for doing what you need, since it is not able to get the return value of the handler.
You can achieve what you don't want without modifying existing code using aspect-oriented programming (AOP). For this to work in spring, you need to enable banks for spring-aop and AspectJ.
Create Aspect and Tips
@Aspect @Component public class ActionResponseLoggerAspect { private static final Logger logger = LoggerFactory.getLogger(ActionResponseLoggerAspect.class); @AfterReturning(pointcut="execution(* your.package.UserController.*(..)))", returning="result") public void afterReturning(JoinPoint joinPoint , Object result) { if (result instanceof ActionResponse) { ActionResponse m = (ActionResponse) result; logger.info("ActionResponse returned with message [{}]", m.getMessage()); } } }
The afterReturning method will be executed each time the controller method is returned.
Enabling @AspectJ Support
Enable AspectJ support by adding this to your XML configuration.
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
See the spring docs for more information.
source share