Last active
December 28, 2020 08:04
-
-
Save susimsek/730f565a0955dd66d581d2f4939f082a to your computer and use it in GitHub Desktop.
Spring Boot Generic Paged Class
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
| # Page Properties | |
| spring.data.web.pageable.default-page-size=${DEFAULT_PAGE_SIZE:1} | |
| spring.data.web.pageable.max-page-size=${MAX_PAGE_SIZE:100} |
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
| public class GenericPagedDto<T> extends PageImpl<T> { | |
| public GenericPagedDto(List<T> content, Pageable pageable, long total) { | |
| super(content, pageable, total); | |
| } | |
| public GenericPagedDto(List<T> content) { | |
| super(content); | |
| } | |
| } |
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
| @RestController | |
| @FieldDefaults(level = AccessLevel.PRIVATE,makeFinal=true) | |
| @RequiredArgsConstructor | |
| @RequestMapping("/versions/1") | |
| public class InvoiceController { | |
| InvoiceService invoiceService; | |
| @GetMapping("/invoices") | |
| @ResponseStatus(HttpStatus.OK) | |
| public GenericPagedDto<InvoiceDto> listInvoice(Pageable page) { | |
| return invoiceService.listInvoice(page); | |
| } | |
| } |
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
| @Slf4j | |
| @Service | |
| @FieldDefaults(level = AccessLevel.PRIVATE) | |
| @FieldDefaults(level = AccessLevel.PRIVATE,makeFinal = true) | |
| public class InvoiceService { | |
| InvoiceRepository invoiceRepository; | |
| @Override | |
| public GenericPagedDto<InvoiceDto> listInvoice(Pageable page) { | |
| Page<Invoice> invoices = invoices = invoiceRepository.findAll(page); | |
| return new GenericPagedDto(invoices | |
| .stream() | |
| .map(invoice -> invoiceMapper.invoiceToInvoiceDtoWithLink(invoice)) | |
| .collect(Collectors.toList()), page,invoices.getTotalElements()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment