处理返回结果 ResponseBodyAdvice

 

 

@ControllerAdvice
public class CommonReturnBodyAnalysis implements ResponseBodyAdvice {

    

 //判断是否要执行beforeBodyWrite方法,true为执行,false不执行

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

//对response处理的执行方法

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
            Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        //通过RequestContextHolder获取request
        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession httpSession = httpServletRequest.getSession(true);
        if(!"logout".equals(returnType.getMethod().getName())) {
            httpSession.setAttribute("body", body);            
        }
        return body;
    }

}
 

你可能感兴趣的:(java,java)