title: How can we implement pagination and sorting without database in Spring Boot
body: <p>I am calling an external REST API and getting response as JSON array. I can implement pagination and sorting when saving and retrieving the same data from a database using JPA as shown below.</p>
  <p>I am using H2 database and created below methods. Is it possible to do the same without using any database? Can we paginate and sort list of objects (created from JSON response) in a similar way?</p>
  <pre class="lang-java prettyprint-override"><code>public class APIResponse&lt;T&gt; {
      int recordCount;
      T response;
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>ResponseEntity&lt;List&lt;Person&gt;&gt; response=restTemplate.exchange(URL, HttpMethod.GET,null, new ParameterizedTypeReference&lt;List&lt;Person&gt;&gt;(){});
  List&lt;Person&gt; persons=response.getBody();
  personService.saveAllPersons(persons);
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>@GetMapping(&quot;/{field}&quot;)
  private  APIResponse&lt;List&lt;Person&gt;&gt; getPersonsWithSort(@PathVariable String field){
      List&lt;Person&gt; personsSorted = personService.findPersonsWithSorting(field);
      return new APIResponse&lt;&gt;(personsSorted.size(),personsSorted);
  }
  
  @GetMapping(&quot;/pagination/{offset}/{pageSize}&quot;)
  private  APIResponse&lt;Page&lt;Person&gt;&gt; getPersonsWithSort(@PathVariable int offset, @PathVariable int pageSize){
      Page&lt;Person&gt; personsPaginated = personService.findPersonsWithPagination(offset, pageSize);
      return new APIResponse&lt;&gt;(personsPaginated.getSize(),personsPaginated);
  }
  
  @GetMapping(&quot;/pagination/{offset}/{pageSize}/{field}&quot;)
  private  APIResponse&lt;Page&lt;Person&gt;&gt; getPersonsWithSort(@PathVariable int offset, @PathVariable int pageSize,@PathVariable String field){
      Page&lt;Person&gt; personsPaginated = personService.findPersonsWithPaginationSorting(offset, pageSize,field);
      return new APIResponse&lt;&gt;(personsPaginated.getSize(),personsPaginated);
  </code></pre>