Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get returned value of my controllers from HandlerInterceptor

I'm creating a log manager for my controllers that logs every action in it and returned values

My controllers are defined in this way:

@Controller
@RequestMapping(value="/ajax/user")
public class UserController extends AbstractController{
  @RequestMapping(value="/signup")
  public @ResponseBody ActionResponse signup(@Valid SignupModel sign) {
    ActionResponse response=new ActionRespone();
    response.setMessage("This is a test message");
    return response;
  }
}

and I defined a HandlerInterceptor to log output of each handler:

@Component
public class ControllerInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        return true;
    }
  public void postHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
            throws Exception {

      LogManager log=new LogManager();
      log.setMessage();//I need returned ActionResponse here
  }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}

where I use log.setMessage(); I need my ActionResponse's message (This is a test message) which is returned from signup method

How can I do this?

like image 798
sinoohe Avatar asked Oct 22 '22 04:10

sinoohe


1 Answers

An interceptor is not the right place to do what you want since it's not capable of getting the return value of the handler.

You can achieve what you wan't without changing any existing code using aspect oriented programming (AOP). For this to work in spring you'll need to include the jars for spring-aop and AspectJ.

Creating the aspect and advice

@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 every time a controller method returns.

Enabling @AspectJ Support

Enable AspectJ support by adding this to your XML configuration.

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

For more info see the spring docs.

like image 62
12 revs Avatar answered Nov 01 '22 10:11

12 revs