title: Sorting, Filtering and Paging Restful using Spring
body: <p>how can i implement a pagination usign Spring?</p>
  
  <p>some advice?</p>
  
  <pre><code>public class ClienteServiceImpl implements ClienteService {
  
  public List&lt;Cliente&gt; findAllClientes(final String ragioneSociale, final String partitaIva, String sortBy, String sortOrder, Integer startResult, Integer maxRows) {
  
      Sort sort = null;
      Specification&lt;Cliente&gt; specification = null;
  
      if(sortBy != null){
          // sorting
          sort = new Sort(Direction.fromString(sortOrder), sortBy);
      }
  
      if(ragioneSociale != null || partitaIva != null){
          // filtering
          specification = new Specification&lt;Cliente&gt;() {
              public Predicate toPredicate(Root&lt;Cliente&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb) {
                  Predicate p1 =  ragioneSociale != null ? cb.like(root.&lt;String&gt;get("ragioneSociale"), ragioneSociale + "%") : cb.isNotNull(root.&lt;Long&gt;get("id"));
                  Predicate p2 =  partitaIva != null ? cb.like(root.&lt;String&gt;get("partitaIva"), partitaIva + "%") : cb.isNotNull(root.&lt;Long&gt;get("id"));
                  return cb.and(p1,p2);
              }
          };
      }
  
      List&lt;Cliente&gt; clientes = new ArrayList&lt;Cliente&gt;();
      if(sort == null &amp;&amp; specification == null) clientes = clienteDAO.findAll();
      else if(sort != null &amp;&amp; specification == null) clientes = clienteDAO.findAll(sort);
      else if(sort == null &amp;&amp; specification != null) clientes = clienteDAO.findAll(specification);
      else if(sort != null &amp;&amp; specification != null) clientes = clienteDAO.findAll(specification, sort);
  
  
  
      return clientes;
  }
  </code></pre>