I have to write some REST service which should handle a million entries and return a response to the user in JSON format. I'm writing some REST-controller using Spring and make pagination.
public List<ContactDto> getAllContacts() { double countItems = contactRepo.count(); int pages = (int) Math.ceil(countItems / totalItemsPerPage); List<Contact> contacts = new ArrayList<>(); for (int i = 0; i < pages; i++) { Page<Contact> page = contactRepo.findAll(PageRequest.of(i, totalItemsPerPage)); contacts.addAll(page.stream().collect(Collectors.toList())); } return contacts.stream() .map(entity -> new ContactDto(entity.getId(), entity.getName())) .collect(Collectors.toList()); } I'm new in spring and pagination.
In this approach is have a sense or I'm doing something wrong?
I mean I want to know I'm using pagination write or wrong?
Thanks for the help!