/**
* Constructor of {@code PageImpl}.
*
* @param content the content of this page, must not be {@literal null}.
* @param pageable the paging information, must not be {@literal null}.
* @param total the total amount of items available. The total might be adapted considering the length of the content
* given, if it is going to be the content of the last page. This is in place to mitigate inconsistencies.
*/
public PageImpl(List<T> content, Pageable pageable, long total)
content:當前頁的記錄。
total:全部記錄數
也就是用PageImpl實現Page接口的時候,不能把數據全取回來放入content,得按分頁的size放入。而最後一個參數須要記錄的是全部數據的計數。
public Page<ESIndexObject> getAllESIndex(Pageable pageable) {
String strQuery = "/_cat/indices?v&h=uuid,health,status,index,docsCount,storeSize,cds&s=cds:desc&format=json";
Request request = new Request("GET", strQuery);
try {
Response response = restClient.performRequest(request);
String responseBody = EntityUtils.toString(response.getEntity());
ObjectMapper mapper = new ObjectMapper();
List<ESIndexObject> list = mapper.readValue(responseBody,new TypeReference<List<ESIndexObject>>(){});
int totalElements = list.size();
if(pageable==null)
{
pageable = PageRequest.of(0,10);
}
int fromIndex = pageable.getPageSize()*pageable.getPageNumber();
int toIndex = pageable.getPageSize()*(pageable.getPageNumber()+1);
if(toIndex>totalElements) toIndex = totalElements;
//若是list的內容取回後基本不變化,能夠考慮放入類對象變量裏或者其餘緩存裏,而後判斷list是否可用,不用每次都去取list,適合取數的時候沒法真分頁只能僞分頁狀況。 //相似取mysql數據庫的狀況,由於數據取數自己支持分頁,因此list的數據每次都取當前頁就能夠,可是須要先要計算全部記錄數,而後傳入totalElements變量
List<ESIndexObject> indexObjects = list.subList(fromIndex,toIndex);#獲取當前頁數據
Page<ESIndexObject> page = new PageImpl<>(indexObjects,pageable,totalElements);
return page;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}