Closed Joseulisses065 closed 11 hours ago
Minha implementação de pagination no header @Joseulisses065
/**
* Method to build an {@link HttpHeaders} with:
* <ul>
* <li>X-Pagination-Page-Index</li>
* <li>X-Pagination-Page-Size</li>
* <li>X-Pagination-Page-Total</li>
* <li>X-Pagination-Item-Total</li>
* <li>Content-Language</li>
* </ul>
*
* @param page An {@link Page} of {@link UUID}. Cannot be null
* @param language An {@link String} with language. Can be null
* @return An {@link HttpHeaders} with all defined params
* @since 1.0.0
*/
public HttpHeaders getHeaders(Page<UUID> page, String language) {
HttpHeaders headers = new HttpHeaders() {{
// We add +1 because of 'one-indexed-parameters' is set to true
add("X-Pagination-Page-Index", String.valueOf(page.getNumber() + 1));
add("X-Pagination-Page-Size", String.valueOf(page.getNumberOfElements()));
add("X-Pagination-Page-Total", String.valueOf(page.getTotalPages()));
add("X-Pagination-Item-Total", String.valueOf(page.getTotalElements()));
}};
if (!isNullOrEmpty(language)) headers.add("Content-Language", language);
return headers;
}
O uso seria algo assim:
@GetMapping
public ResponseEntity<List<T>> findAll(Pageable pageable) {
// Page<T> pagination = service.findAll(pageable)
// List<T> res = pagination.getContent()
HttpHeaders headers = responseUtils.getHeaders(pagination, null);
return ResponseEntity.ok().headers(headers).body(res);
}
Descrição
Issues
Adicionar paginação aos endpoints findAll na api