0% found this document useful (0 votes)
37 views13 pages

Pagination and Sorting Using Spring Boot - by Chakresh Tiwari - ShoutLoudz - Medium

pagination and sorting in spring boot

Uploaded by

mibomoh669
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views13 pages

Pagination and Sorting Using Spring Boot - by Chakresh Tiwari - ShoutLoudz - Medium

pagination and sorting in spring boot

Uploaded by

mibomoh669
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Pagination and Sorting using Spring Boot | by Chakresh Tiwari | Shout... https://medium.com/shoutloudz/pagination-and-sorting-using-spring-bo...

Open in app Sign up Sign in

Search

Pagination and Sorting using Spring Boot


Chakresh Tiwari · Follow
Published in ShoutLoudz
3 min read · Jul 8, 2022

Listen Share

Photo by Jan Antonin Kolar on Unsplash

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.

To overcome this problem there are multiple ways like -

1. We can have filters in our queries which are getting data.

2. We can set the limit

3. We can have pagination

So in this post, we will be discussing pagination and when it is required.

It is required wherever we are showing data in tabular format in UI, So instead of


getting all the data in one shot and showing only 50 records is not a good idea,
instead, we can use pagination in such scenarios and we can fetch relevant data
quickly.

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.

Development using Spring Boot

Now in the development part, we will follow the below steps-

1. Create a spring boot project from spring initializer

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:

In spring boot for implementing paging and sorting, we need to extend


PagingAndSortingRepository in our UserRepository interface.

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 }

pagingrepo.java hosted with by GitHub view raw

PagingAndSortingRepository is an extension of CrudRepository.

PagingAndSortingRepository has two methods like

Page<T> findAll(Pageable pageable);


Iterable<T> findAll(Sort sort);

Now findAll() method will take Pageable in argument. PageRequest implements


Pageable.

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 }

paging.java hosted with by GitHub view raw

From controller we will send request like this

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 }

pagingcontroller.java hosted with by GitHub view raw

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.

PageRequest pageRequest = PageRequest.of(pageNo, pageSize,


Sort.by("name").ascending());

For sorting as we have seen PagingAndSortingRepository have one findAll() method


which will take Sort-Object in the argument. For sorting, we need to select by which
property we want to sort our results we can select multiple properties as well. e.g.

Sort nameSort = Sort.by("name");


Sort emailSort = Sort.by("email");

Sort multiSort = emailSort.and(nameSort);

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...

List<EmployeeEntity> result = repository.findAll(multiSort);

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> {

public Slice<User> findByEmail(String email, Pageable pageable);


}
In Service layer Method:

Slice<User> result = userRepository.findByEmail("[email protected]",


paging);

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.

You can check out the code on Github.

Thanks for reading!!

LinkedIn

Spring Java Programming Coding Spring Boot

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

Written by Chakresh Tiwari


661 Followers · Editor for ShoutLoudz

Software Engineer at Cisco(Appdynamics) , Sharing my knowledge and experience related to work. I am here
to help learners to prepare for tech interviews.

More from Chakresh Tiwari and ShoutLoudz

Chakresh Tiwari in ShoutLoudz

Spring Boot: Upload and Download Images using JPA


In this post we are going discuss about How to upload an Image into Database, and then
download it back. We will be using Spring Boot…

Jul 31, 2022 254 10

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...

Chakresh Tiwari in ShoutLoudz

System Design Questions and Resources for SDE I/II


Introduction

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...

Chakresh Tiwari in ShoutLoudz

System Design HLD vs LLD


In this post, we are going to discuss different kind of designs which we keep in mind while
developing any application.

Apr 10, 2021 65 1

Chakresh Tiwari in ShoutLoudz

Elastic Search with Spring Boot


In the first part of the elastic search series, I have explained about basics of elastic search and
using it with Spring boot.

Jul 24, 2022 32 1

See all from Chakresh Tiwari

See all from ShoutLoudz

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...

Recommended from Medium

Vinotech

Dynamic Searching with Spring Data JPA Specification


In Java Persistence API (JPA), a specification is a set of interfaces and classes that define the
programming contracts between a…

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

Implementing @OneToMany and @ManyToOne in Spring Boot


Application
In this article, I will demo very simple use of @OneToMany and @ManyToOne.

May 4 23

Lists

General Coding Knowledge


20 stories · 1595 saves

Stories to Help You Grow as a Software Developer


19 stories · 1378 saves

Coding & Development


11 stories · 820 saves

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

Coding Interview Question Based Solving Real-Time Example Using Java


8 Features
In this article, we explore solving real-time queries using Java 8 features, with a focus on a
School and Teacher example. We will utilize…

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

Ultimate Guide to N+1 Loading Problem in Hibernate/JPA.


Welcome to the second installment of the series on Hibernate/JPA in Spring! Before diving into
this article, I recommend checking out the…

Apr 22 271 5

Mpavani

Microservices Must Know Interview Questions(Scenario Based part-2)


1. Scenario: A microservice in your system must perform a time-consuming task. How do you
ensure that other services don’t experience…

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

See more recommendations

13 of 13 9/27/2024, 8:19 PM

You might also like