循环分页获取全部数据
1 /** 2 * 循环分页获取北森出差数据,当最后一次调用获取的数据量不足pageSize则表明是最后一页 3 * @param travelDataRequest 出差数据Http请求Request 4 */ 5 private List<BusinessData> getAllPageDate(TravelDataRequest travelDataRequest) { 6 List<BusinessData> businessDataList = Lists.newArrayList(); 7 try { 8 int i = ONE; 9 while (true) { 10 travelDataRequest.setPageIndex(i); 11 travelDataRequest.setPageSize(PAGE_SIZE); 12 Config config = ConfigService.getConfig(ConstantsUtil.NAME_SPACE_KEY); 13 String url = config.getProperty(URL,""); 14 logger.info("从阿波罗获取到出差数据接口URL为:[{}]", url); 15 String result = HttpClientUtil.httpPostJson(url, JSON.toJSONString(travelDataRequest) , null).getResult(); 16 TravelDataResponse travelDataResponse = JSON.parseObject(result, TravelDataResponse.class); 17 TravelDataResponse.Data data = Optional.ofNullable(travelDataResponse).map(TravelDataResponse::getData) 18 .orElseThrow(() -> new IllegalStateException("获取数据为空")); 19 List<BusinessData> businessList = data.getBusinessList(); 20 Integer total = data.getTotal(); 21 businessDataList.addAll(businessList); 22 23 //如果获取当前页的数据总量小于每页大小,则表明是最后一页跳出循环 24 if (CollectionUtil.size(businessList) < PAGE_SIZE) { 25 logger.info("从北森获取出差数据总共[{}]页", i); 26 break; 27 } 28 29 //如果获取到的数据总量大于等于total则跳出循环 30 if (businessDataList.size() >= total) { 31 break; 32 } 33 34 i++; 35 } 36 return businessDataList; 37 } catch (Exception e) { 38 logger.error("调用北森接口获取出差数据出现异常", e); 39 return Lists.newArrayList(); 40 } 41 }