拦截器获取Controller的返回值

  • Post author:
  • Post category:其他




拦截器获取Controller的返回值
@ControllerAdvice
public class LoginLogAnalysis implements ResponseBodyAdvice {

    private static final String LOGIN_URI = "/login";

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {

        String requestPath = request.getURI().getPath();
		
       //获取指定访问接口的返回值
        if(requestPath.equals(LOGIN_URI)){
			//通过RequestContextHolder获取request
            HttpServletRequest httpServletRequest = 
                ((ServletRequestAttributes) RequestContextHolder                                                  .getRequestAttributes()).getRequest();

            HttpSession httpSession = httpServletRequest.getSession(true);
            httpSession.setAttribute("body", body);

            return body;
        }
        return body;
    }
}
public class LoginInterceptor implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(LoginInterceptor.class);


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

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

        //获取Controller返回值
        Map logBodyMap = (Map) request.getSession().getAttribute("body");
        
        log.info("输出日志信息");

    }
}



版权声明:本文为Maktub_J原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。