How to query data out of the box using Spring data JPA by both Sort and Pageable

Spring Boot

Developing REST API? Clients wants both sort & pagination?

That’s quite simple btw!

Since last few hours was trying to add this simple feature my Spring Boot project. In my project pagination was done using pageable

In this tutorial I’m gonna show how to achieve this.

Let’s copy the code from below:

public List<Model> getAllData(Pageable pageable){
       List<Model> models= new ArrayList<>();
       modelRepository.findAllByOrderByIdDesc(pageable).forEach(models::add);
       return models;
   }
//Model = Your Model where you want to implement this feature. 

That’s all! We’re done!
If you’ve any confusion please let me know 🙂

2 thoughts to “How to query data out of the box using Spring data JPA by both Sort and Pageable”

Leave a Reply

Your email address will not be published. Required fields are marked *