title: How to make pagination in the REST service?
body: <p>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.</p>
  
  <pre><code>public List&lt;ContactDto&gt; getAllContacts() {
  
          double countItems = contactRepo.count();
          int pages = (int) Math.ceil(countItems / totalItemsPerPage);
  
          List&lt;Contact&gt; contacts = new ArrayList&lt;&gt;();
          for (int i = 0; i &lt; pages; i++) {
              Page&lt;Contact&gt; page = contactRepo.findAll(PageRequest.of(i, totalItemsPerPage));
              contacts.addAll(page.stream().collect(Collectors.toList()));
          }
  
          return contacts.stream()
                  .map(entity -&gt; new ContactDto(entity.getId(), entity.getName()))
                  .collect(Collectors.toList());
      }
  </code></pre>
  
  <p>I'm new in spring and pagination. </p>
  
  <p>In this approach is have a sense or I'm doing something wrong? </p>
  
  <p>I mean I want to know I'm using pagination write or wrong?</p>
  
  <p>Thanks for the help!</p>