RequestContextHolder分析及Java代码

1:SpringMVC提供的RequestContextHolder详解

1.RequestContextHolder的使用

RequestContextHolder顾名思义,持有上下文的Request容器.使用是很简单的,具体使用如下:

Java代码具体实现:

/**
 * 基础控制器类
 * 
 */
public class BaseController {
    /**
     * 获取 HttpServletResponse
     */
    public static HttpServletResponse getResponse() {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        return response;
    }

    /**
     * 获取 包装防Xss Sql注入的 HttpServletRequest
     * @return request
     */
    public static HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return new WafRequestWrapper(request);
    }

    /**
     * 包装一个list,让list增加额外属性
     */
    protected Object warpObject(BaseControllerWarpper warpper) {
        return warpper.warp();
    }
}

(二)

/**
 * Created with IntelliJ IDEA.
 * Description: 控制器查询结果的包装类基类
 * User: 
 * Date: 2017-08-02
 * Time: 12:54
 */
public abstract class BaseControllerWarpper {
    public Object obj = null;

    public BaseControllerWarpper(Object obj) {
        this.obj = obj;
    }
    @SuppressWarnings("unchecked")
    public Object warp() {
        if (this.obj instanceof List) {
            List<Map<String, Object>> list = (List<Map<String, Object>>) this.obj;
            for (Map<String, Object> map : list) {
                warpTheMap(map);
            }
            return list;
        } else if (this.obj instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) this.obj;
            warpTheMap(map);
            return map;
        } else {
            return this.obj;
        }
    }

    protected abstract void warpTheMap(Map<String, Object> map);
}

<三>

@Controller
@RequestMapping("/app/bbb")
@Api(basePath = "/app/bbb", description = "xx", position = 1)
public class FlowAnalysisController extends BaseController {
    //log
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    public FlowAnalysisService flowAnalysisService;

    @ApiOperation(value = "5.1 bbb", httpMethod = "GET", notes = " 说明:retCode 0:失败 1:成功 retDesc:状态描述 retData:返回数据 返回结果示例:", response = String.class)
    @RequestMapping(value = "/compareTreadStat", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    /**
     * 趋势统计数据
     */
    public String compareTreadStat(@ApiParam(required = true, name = "dateType", value = "日期类型", defaultValue = "real", allowableValues = "real,day,week,month") @RequestParam(required = true) String dateType,
                                   @ApiParam(required = false, name = "startTime", value = "开始时间", defaultValue = "2017-06-20") @RequestParam(required = false) String startTime,
                                   @ApiParam(required = false, name = "endTime", value = "结束时间", defaultValue = "2017-06-25") @RequestParam(required = false) String endTime,
                                   @ApiParam(required = true, name = "storeCode", value = "门店编码", defaultValue = "871L") @RequestParam(required = true) String storeCode
    ) {
        long begin = System.currentTimeMillis();
        logger.info("treadStat dateType:" + dateType + " startTime:" + startTime + " endTime:" + endTime + " storeCode:" + storeCode);

        //默认返回成功
        Map<String, Object> result = PassengerFlowCountingUIUtil.getSuccessResult();
        try {
            FlowAnalysisQueryParam queryParam = new FlowAnalysisQueryParam(dateType, startTime, endTime, storeCode);
            //获取数据列表
            FlowAnalysisTreadStatVO flowAnalysisTreadStatVO1 = flowAnalysisService.treadStat(queryParam);

//            FlowAnalysisTreadStatVO flowAnalysisTreadStatVO2 = flowAnalysisService.last1TreadStat(queryParam);
//
//            FlowAnalysisTreadStatVO flowAnalysisTreadStatVO3 = flowAnalysisService.last2TreadStat(queryParam);
            long costTime = System.currentTimeMillis() - begin;
            PassengerFlowCountingUIUtil.addElement(result, "cost", String.valueOf(costTime) + "ms");
            //列表添加到返回结果中
            PassengerFlowCountingUIUtil.addReturnData(result, flowAnalysisTreadStatVO1);
        } catch (Exception e) {
            //获取错误结果
            result = PassengerFlowCountingUIUtil.getFailedResult(e.getMessage());
            logger.error("compareTreadStat error:", ExceptionUtils.getStackTrace(e));
        }
        logger.info("web treadStat costs:" + (System.currentTimeMillis() - begin) + " ms");
        return PassengerFlowCountingUIUtil.toJSONString(result);
    }

    @ApiOperation(value = "5.2 cccc", httpMethod = "GET", notes = " 说明:retCode 0:失败 1:成功 retDesc:状态描述 retData:返回数据 返回结果示例:", response = String.class)
    @RequestMapping(value = "/inOutRetentionTreadStat", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    /**
     * 趋势统计数据
     */
    public String inOutRetentionTreadStat(@ApiParam(required = true, name = "dateType", value = "日期类型", defaultValue = "real", allowableValues = "real,day,week,month") @RequestParam(required = true) String dateType,
                                          @ApiParam(required = false, name = "startTime", value = "开始时间", defaultValue = "2017-06-20") @RequestParam(required = false) String startTime,
                                          @ApiParam(required = false, name = "endTime", value = "结束时间", defaultValue = "2017-06-25") @RequestParam(required = false) String endTime,
                                          @ApiParam(required = true, name = "storeCode", value = "门店编码", defaultValue = "871L") @RequestParam(required = true) String storeCode
    ) {
        long begin = System.currentTimeMillis();
        logger.info("treadStat dateType:" + dateType + " startTime:" + startTime + " endTime:" + endTime + " storeCode:" + storeCode);

        //默认返回成功
        Map<String, Object> result = PassengerFlowCountingUIUtil.getSuccessResult();
        try {
            FlowAnalysisQueryParam queryParam = new FlowAnalysisQueryParam(dateType, startTime, endTime, storeCode);
            //获取数据列表
            FlowAnalysisTreadStatVO flowAnalysisTreadStatVO1 = flowAnalysisService.treadStat1(queryParam);
            long costTime = System.currentTimeMillis() - begin;
            PassengerFlowCountingUIUtil.addElement(result, "cost", String.valueOf(costTime) + "ms");
            //列表添加到返回结果中
            PassengerFlowCountingUIUtil.addReturnData(result, flowAnalysisTreadStatVO1);
        } catch (Exception e) {
            //获取错误结果
            result = PassengerFlowCountingUIUtil.getFailedResult(e.getMessage());
            logger.error("inOutRetentionTreadStat error:", ExceptionUtils.getStackTrace(e));
        }
        logger.info("web treadStat costs:" + (System.currentTimeMillis() - begin) + " ms");
        return PassengerFlowCountingUIUtil.toJSONString(result);
    }
    @ApiOperation(value = "5.3 客流通道", httpMethod = "GET", notes = " 说明:retCode 0:失败 1:成功 retDesc:状态描述 retData:返回数据 返回结果示例:", response = String.class)
    @RequestMapping(value = "/doorData", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    /**
     * 趋势统计数据
     */
    public String doorData(@ApiParam(required = true, name = "dateType", value = "日期类型", defaultValue = "real", allowableValues = "real,day,week,month") @RequestParam(required = true) String dateType,
                             @ApiParam(required = false, name = "startTime", value = "开始时间", defaultValue = "2017-06-20") @RequestParam(required = false) String startTime,
                             @ApiParam(required = false, name = "endTime", value = "结束时间", defaultValue = "2017-06-25") @RequestParam(required = false) String endTime,
                             @ApiParam(required = true, name = "storeCode", value = "门店编码", defaultValue = "871L") @RequestParam(required = true) String storeCode
    ) {
        long begin = System.currentTimeMillis();
        logger.info("treadStat dateType:" + dateType + " startTime:" + startTime + " endTime:" + endTime + " storeCode:" + storeCode);

        //默认返回成功
        Map<String, Object> result = PassengerFlowCountingUIUtil.getSuccessResult();
        try {
            FlowAnalysisQueryParam queryParam = new FlowAnalysisQueryParam(dateType, startTime, endTime, storeCode);
            //获取数据列表
            FlowAnalysisTreadStatVO flowAnalysisTreadStatVO = flowAnalysisService.doorCount(queryParam);
            long costTime = System.currentTimeMillis() - begin;
            PassengerFlowCountingUIUtil.addElement(result, "cost", String.valueOf(costTime) + "ms");
            //列表添加到返回结果中
            PassengerFlowCountingUIUtil.addReturnData(result, flowAnalysisTreadStatVO);
        } catch (Exception e) {
            //获取错误结果
            result = PassengerFlowCountingUIUtil.getFailedResult(e.getMessage());
            logger.error("doorData error:", ExceptionUtils.getStackTrace(e));
        }
        logger.info("web treadStat costs:" + (System.currentTimeMillis() - begin) + " ms");
        return PassengerFlowCountingUIUtil.toJSONString(result);
    }

    @ApiOperation(value = "5.4 客流通道排名", httpMethod = "GET", notes = " 说明:retCode 0:失败 1:成功 retDesc:状态描述 retData:返回数据 返回结果示例:", response = String.class)
    @RequestMapping(value = "/doorDataRank", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    /**
     * 趋势统计数据
     */
    public String doorDataRank(@ApiParam(required = true, name = "dateType", value = "日期类型", defaultValue = "real", allowableValues = "real,day,week,month") @RequestParam(required = true) String dateType,
                             @ApiParam(required = false, name = "startTime", value = "开始时间", defaultValue = "2017-06-10") @RequestParam(required = false) String startTime,
                             @ApiParam(required = false, name = "endTime", value = "结束时间", defaultValue = "2017-06-20") @RequestParam(required = false) String endTime,
                             @ApiParam(required = true, name = "storeCode", value = "门店编码", defaultValue = "871L") @RequestParam(required = true) String storeCode
    ) {
        long begin = System.currentTimeMillis();
        logger.info("treadStat dateType:" + dateType + " startTime:" + startTime + " endTime:" + endTime + " storeCode:" + storeCode);

        //默认返回成功
        Map<String, Object> result = PassengerFlowCountingUIUtil.getSuccessResult();
        try {
            FlowAnalysisQueryParam queryParam = new FlowAnalysisQueryParam(dateType, startTime, endTime, storeCode);
            //获取数据列表
            FlowAnalysisTreadStatVO flowAnalysisTreadStatVO = flowAnalysisService.doorCountRank(queryParam);
            long costTime = System.currentTimeMillis() - begin;
            PassengerFlowCountingUIUtil.addElement(result, "cost", String.valueOf(costTime) + "ms");
            //列表添加到返回结果中
            PassengerFlowCountingUIUtil.addReturnData(result, flowAnalysisTreadStatVO);
        } catch (Exception e) {
            //获取错误结果
            result = PassengerFlowCountingUIUtil.getFailedResult(e.getMessage());
            logger.error("doorDataRank error:", ExceptionUtils.getStackTrace(e));
        }
        logger.info("web treadStat costs:" + (System.currentTimeMillis() - begin) + " ms");
        return PassengerFlowCountingUIUtil.toJSONString(result);
    }
    public FlowAnalysisService getFlowAnalysisService() {
        return flowAnalysisService;
    }

    public void setFlowAnalysisService(FlowAnalysisService flowAnalysisService) {
        this.flowAnalysisService = flowAnalysisService;
    }
}



































































































































你可能感兴趣的:(项目总结)