Pagination and Sorting Using Spring Boot - by Chakresh Tiwari - ShoutLoudz - Medium
Pagination and Sorting Using Spring Boot - by Chakresh Tiwari - ShoutLoudz - Medium
Search
Listen Share
While developing any spring boot API, We create many rest endpoints for the CRUD
operation on data. After some time when a large number of records get inserted into
1 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
DB, then fetching the records from DB takes more time, because it fetches all the
records.
Similar way for sorting we can sort the data on the UI side also but it will be a not
good idea because of performance, So we can sort it from the backend just by
passing column names by which we want to sort the records.
2. Create different packages like (entity, model, repo, service, controller, config,
util etc.)
3. Create a few crud endpoints (Like create a user, get all User)
Paging:
2 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
1 @Repository
2 public interface UserRepository extends PagingAndSortingRepository<User, Integer> {
3 }
1 @Override
2 public List<User> getUsersByPagination(int pageNo, int pageSize) {
3
4 //create pagerequest object
5 PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
6 //pass it to repos
7 Page<User> pagingUser = userRepository.findAll(pageRequest);
8 //pagingUser.hasContent(); -- to check pages are there or not
9 return pagingUser.getContent();
10 }
3 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
1 @GetMapping
2 public List<User> getUserWithPaging(@RequestParam(defaultValue = "0") Integer pageNo,
3 @RequestParam(defaultValue = "10") Integer pageSize){
4
5 return userService.getUsersByPagination(pageNo,pageSize);
6
7 }
Now for testing, we can hit this URL from the postman.
http://localhost:8083/api/user?pageNo=0&pageSize=5
Page Number starts from zero. and page Size is the number of records per page.
Sometimes the user doesn't want to pass these values from the URL that's why I have
added default values.
Sorting:
We can apply Paging and sorting together as well one at a time also.
The above example is about only paging. Next is paging and sorting together. the
default order of sorting is descending.
4 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
Slice:
Slice is similar to page only difference is while using Page it has extra data like the
first page, total page, pageNumber, and count so calculating this takes extra time
internally. whereas Slice is having only data and no extra info, so performance-wise
it is better.
@Repository
public interface UserRepository extends CrudRepository<User,
Integer> {
So this is all about paging and sorting using Spring Boot, It is very useful while doing
projects as well as for interviews also.
So I hope now you understood how to use it in projects If any doubts let me know.
5 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
Follow
Software Engineer at Cisco(Appdynamics) , Sharing my knowledge and experience related to work. I am here
to help learners to prepare for tech interviews.
6 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
May 1, 2022 24
7 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
8 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
Vinotech
Jun 25 14
9 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
Mohit Sehgal
May 4 23
Lists
ChatGPT
21 stories · 815 saves
10 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
Sanjay Singh
Aug 15 208 4
11 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
Chi Kim
Apr 22 271 5
Mpavani
Sep 20 14
12 of 13 9/27/2024, 8:19 PM
Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...
Mikael Svens
Effortless Caching With Caffeine in Spring Boot (A Must Use For API
Caching)
Apr 4 116
13 of 13 9/27/2024, 8:19 PM