How to handle pagination and sorting in Spring Data?

Photo by Pixabay on

Let’s say you want to handle pagination and sorting for your application and you don’t want to do the hard work of writing SQL queries or setting criteria using an ORM tool.

Spring Data provides a very simple and efficient way to handle pagination and sorting.

All you have to do is create a repository interface which extends PaginationAndSortingRepository interface provided by Spring Data!

Here are the steps (using Spring Boot):

STEP 1: Add dependencies:

Add spring data dependency:


In addition I have added spring web dependency to test my changes through a REST service and in memory database H2 dependency to perform the database operations.

Here is the entire pom :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
		<relativePath /> <!-- lookup parent from repository -->
	<description>Demo project for Spring Boot Pagination</description>






STEP 2: Create the entity class

package com.springboot.pagination;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

public class Order {

	private Long id;

	private String name;

	private float price;

	private int quantity;

	private String category;

	 * @return the id
	public Long getId() {
		return id;

	 * @param id the id to set
	public void setId(Long id) { = id;

	 * @return the name
	public String getName() {
		return name;

	 * @param name the name to set
	public void setName(String name) { = name;

	 * @return the price
	public float getPrice() {
		return price;

	 * @param price the price to set
	public void setPrice(float price) {
		this.price = price;

	 * @return the quantity
	public int getQuantity() {
		return quantity;

	 * @param quantity the quantity to set
	public void setQuantity(int quantity) {
		this.quantity = quantity;

	 * @return the category
	public String getCategory() {
		return category;

	 * @param category the category to set
	public void setCategory(String category) {
		this.category = category;


I have created an Order entity class with an id , name , price , quantity and category fields.

Note that I have mentioned the table name as order_table . ‘Order’ is a restricted word in SQL so you cannot use it to create a table . So I manually entered the table name instead of skipping it else Spring Data automatically tries to generate the table using the entity name.

STEP 3 – Create the repository

Here is where the prime action takes place.

Create a repository which extends PaginationAndSortingRepository provided by Spring Data.

package com.springboot.pagination;


public interface OrderRepository extends PagingAndSortingRepository<Order, Long> {


The entity class name ‘Order’ and the data type of the identifier ‘Long’ are passed as generic parameters to the interface.

Just creating this interface lets you enjoy the pagination and sorting features of Spring Data.

Now let’s make use of it.

STEP 4 – Create the client code

I have created a REST controller client which will invoke the repository.

I have made the page number , page size and the field on which to sort by , all customizable.

Here is the code :

package com.springboot.pagination;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

public class OrderController {

	private OrderRepository repo;

	public List<Order> getOrders(@RequestBody Map<String, String> input) {

		int pageNo = Integer.parseInt(input.get("pageNo"));

		int pageSize = Integer.parseInt(input.get("pageSize"));

		Sort sortBy ="sortBy"));

		PageRequest pageRequest= PageRequest.of(pageNo, pageSize, sortBy);

		Page<Order> pages = repo.findAll(pageRequest);

		return pages.getContent();



PaginationAndSortingRepository provides a method findAll() which takes an instance of ‘PageRequest’ as a parameter.

It’s in this pageRequest instance that we set our pagination and sorting parameters.

Let’s say you want the first page among pages of size 5.

You can create a page request instance this way:

PageRequest pageRequest = PageRequest.of(0,5);

The values starts from 0 , so the first page is denoted by 0 . The second parameter takes the page size.

In the controller code , I have fetched this from the input passed by the client .

To sort based on a field just add to the page request. This can be dropped if sorting is not required.

By default sorting is in ascending order. You can change it to descending by doing this :


Now let’s test the changes.

I have inserted few records through data.sql file kept under resources folder.

I have used in memory database by adding a dependency in the pom.xml

Here is the data.sql:

insert into order_table(id,name,price,quantity,category) values(1,'Almonds - 100 gm pack',300.0,200,'Nuts');
insert into order_table(id,name,price,quantity,category) values(2,'Raisins - 100 gm pack',200.0,100,'Nuts');
insert into order_table(id,name,price,quantity,category) values(3,'Onion 1 kg pack',150,50,'Vegetables');
insert into order_table(id,name,price,quantity,category) values(4,'Banana 10 kg back',2000,40,'Vegetables');
insert into order_table(id,name,price,quantity,category) values(5,'Apple 200 gm pack',500,80,'Vegetables');
insert into order_table(id,name,price,quantity,category) values(6,'Iphone',30000,100,'Electronics');
insert into order_table(id,name,price,quantity,category) values(7,'Boss Headphone',8000,180,'Electronics');
insert into order_table(id,name,price,quantity,category) values(8,'Iphone Charger',1000,200,'Electronics');
insert into order_table(id,name,price,quantity,category) values(9,'Macbook',300000,100,'Electronics');
insert into order_table(id,name,price,quantity,category) values(10,'Trimmer',3000,700,'Electronics');

I have not added anything in file as Spring Boot automatically creates a database named ‘testdb’ on seeing the H2 database dependency in pom file.

Here is a sample request through postman rest client:

And here is the output:

        "id": 4,
        "name": "Banana 10 kg back",
        "price": 2000.0,
        "quantity": 40,
        "category": "Vegetables"
        "id": 10,
        "name": "Trimmer",
        "price": 3000.0,
        "quantity": 700,
        "category": "Electronics"
        "id": 7,
        "name": "Boss Headphone",
        "price": 8000.0,
        "quantity": 180,
        "category": "Electronics"
        "id": 6,
        "name": "Iphone",
        "price": 30000.0,
        "quantity": 100,
        "category": "Electronics"
        "id": 9,
        "name": "Macbook",
        "price": 300000.0,
        "quantity": 100,
        "category": "Electronics"

Below is the algorithm executed by Spring Data:

First it sorts all the items by their price.

And then it divides the records into pages of size 5.

And then it fetches the second page .

And so I have got the costliest five items among the records added .

And they are sorted in ascending order.

2 thoughts

  1. How does spring boot handle keyset pagination? Offset pagination offers poor performance for large scale tables


  2. Spring Boot doesn’t provide a choice to select the pagination strategy. We can go only with their default implementation which is offset pagination.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s