在controller处理完请求之后返回页面渲染之前拦截返回值

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import com.loan.basedao.IRedisUtil;
import com.loan.basedao.impl.RedisUtilImpl;
import com.loan.common.CommUtil;
import com.loan.kafka.impl.SendMsgServerImpl;
import com.loan.mongo.entity.SysLog;
import net.sf.json.JSONObject;
/**
 * 用于修改resonse返回的数据和发送mq因为在拦截器里面发送了请求开始的kafaka,然后在处理完请求之后也会发送kafka这样一来便于统计访问接口的日志和请求的时间
 * @author Administrator
 *
 */
@ControllerAdvice
public class ResponseController implements ResponseBodyAdvice<Object>{
    @Autowired
    private SendMsgServerImpl sendMsgServer;
    @Resource(type = RedisUtilImpl.class)
    private IRedisUtil redisParent;
    
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }
    /**
     * 此方法是拦截返回值,并且操作返回值
     * @param body
     * @param returnType
     * @param selectedContentType
     * @param selectedConverterType
     * @param request
     * @param response
     * @return
     */
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
            Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
            ServerHttpResponse response) {
        System.out.println("进入操作返回方法");
        ServletServerHttpRequest request3 = (ServletServerHttpRequest)request;
        HttpServletRequest request4 = request3.getServletRequest();
        //uuid
        String uuid = CommUtil.toString(request4.getHeader("uuid"));
        //获取responsebody参数
        JSONObject json = JSONObject.fromObject(body);
        String responses =json.toString();
        //结束时间
        long endTime=System.currentTimeMillis();
        //开始时间
        long beginTime=0;
        if(redisParent.containsKey(uuid+"INTERCEPTOR_TIME")){
            beginTime =(long)redisParent.get(uuid+"INTERCEPTOR_TIME");
            //耗时
            //发送kafka日志
            SysLog sysLog = new SysLog();
            sysLog.setUuid(uuid);//uuid
            sysLog.setType("3");//类型
            sysLog.setLogBegTime(CommUtil.stampToDate(String.valueOf(beginTime)));//开始时间
            sysLog.setLogEndTime(CommUtil.stampToDate(String.valueOf(endTime)));//结束时间
            sysLog.setUseTime(String.valueOf(endTime-beginTime));//耗时
            sysLog.setResponse(responses);//返回数据
            sendMsgServer.sendMsg("sysLogTopic", sysLog);
            System.out.println("===================================控制器返回发送消息成功");
            redisParent.remove(uuid+"INTERCEPTOR_TIME");
        }
        
        return body;
    }
}

 

posted @ 2018-10-09 16:10  好久不见你不知道的事  阅读(515)  评论(0编辑  收藏  举报