how to get returned value of my controllers from HandlerInterceptor

元气小坏坏 提交于 2020-05-13 04:34:22

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17813457/how-to-get-returned-value-of-my-controllers-from-handlerinterceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!