Created
August 7, 2026 05:24
-
-
Save Rankarusu/e63cbdf4173f9058c9bb2fcb3d938778 to your computer and use it in GitHub Desktop.
Pagination in Quarkus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // use with @Valid @BeanParam in your resource | |
| public record PaginationQuery( | |
| @RestQuery @DefaultValue("1") @Min(1) int page, | |
| @RestQuery @DefaultValue("50") @Max(200) int pageSize) { | |
| } | |
| // wrap your response in this | |
| public record PaginationMeta<T>( | |
| int page, | |
| int pageSize, | |
| int pageCount, | |
| long totalCount, | |
| List<T> items) { | |
| public static <T> PaginationMeta<T> of(PanacheQuery<T> query, PaginationQuery pagination) { | |
| var page = pagination.page(); | |
| var pageSize = pagination.pageSize(); | |
| if (page < 1) { | |
| throw new IllegalArgumentException("page needs to be >= 1"); | |
| } | |
| var paged = query.page(Page.of(page - 1, pageSize)); | |
| List<T> items = paged.list(); | |
| return new PaginationMeta<>(page, pageSize, paged.pageCount(), paged.count(), items); | |
| } | |
| public <R> PaginationMeta<R> map(Function<T, R> mapper) { | |
| return new PaginationMeta<>(page, pageSize, pageCount, totalCount, items.stream().map(mapper).toList()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment