zoukankan      html  css  js  c++  java
  • 在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;
        }
    }
  • 相关阅读:
    CSS文本超出指定行数省略显示
    Vue框架H5商城类项目商品详情点击返回弹出推荐商品弹窗的实现方案
    vue项目强制清除页面缓存
    使用es6的then()方法封装jquery的ajax请求
    微信小程序——微信卡券的领取和查看
    vue项目如何通过前端实现自动识别并配置服务器环境地址
    HTML中的Meta标签详解
    复用微信小程序源码包后仍然有原小程序的版本管理怎么处理
    译: 3. Axis2快速入门指南
    译: 2. Apache Axis2安装指南
  • 原文地址:https://www.cnblogs.com/oushiyang/p/9760916.html
Copyright © 2011-2022 走看看