Spring MVC – Pagination with Example
We will be explaining how we can implement pagination in Spring MVC Application. This is required when we need to show a lot of data on pages. Suppose in an e-commerce site we have a lot of products but we can’t show all of those on a single page, so we will show only 20 products on each page. This will help in better product tracking and a great user experience. Paging can be implemented using 2 ways:
- From scratch – This is done by querying the database using the offset and the pageSize. This is only used if we need to customize paging more than pageSize and offset.
- Using precompiled repositories – This is done by extending Repositories that have implemented pagination using scratch. This is more generally used.
We will be using the second method by extending from the PagingAndSortingRepository interface for example, but we can use JpaRepository also for the same. These interfaces have the implementations to do paging and sorting.
package org.springframework.data.repository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; @NoRepositoryBean public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> { Iterable<T> findAll(Sort sort); Page<T> findAll(Pageable pageable); }
We will get the page from these functions by passing the Pageable instance.
Steps to Implement Pagination
Setup the database and create an entity.
Java
package com.geeksforgeeks.Spring.MVC.Pagination.data; import lombok.Data; import javax.persistence.*; @Entity @Table @Data public class Product { @Id @Column (name = "id" ) @GeneratedValue (strategy = GenerationType.IDENTITY) private int id; @Column (name = "price" ) private int price; @Column (name = "name" ) private String name; } |
Create a Repository interface extending PagingAndSortingRepository.
Java
package com.geeksforgeeks.Spring.MVC.Pagination.repositories; import com.geeksforgeeks.Spring.MVC.Pagination.data.Product; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.stereotype.Repository; // we can also extend from JpaRepository here @Repository public interface ProductRepository extends PagingAndSortingRepository<Product,Integer> { } |
Create a service that will perform operations on the data retrieved from the repository. Here we will pass the pageable instance in the findAll function and that will return a page.
Java
package com.geeksforgeeks.Spring.MVC.Pagination.services; import com.geeksforgeeks.Spring.MVC.Pagination.data.Product; import com.geeksforgeeks.Spring.MVC.Pagination.repositories.ProductRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import java.util.List; @Service public class ProductService { @Autowired private ProductRepository productRepository; public void saveAllProducts(List<Product>products){ productRepository.saveAll(products); } public Iterable<Product> getAllProducts(Integer pageSize,Integer offset) { return productRepository.findAll(PageRequest.of(offset,pageSize)); } } |
Create a rest api that will show the data in the pages. Here we will receive the page and give the response to the user
Java
package com.geeksforgeeks.Spring.MVC.Pagination.controllers; import com.geeksforgeeks.Spring.MVC.Pagination.data.Product; import com.geeksforgeeks.Spring.MVC.Pagination.services.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.PostConstruct; import javax.websocket.server.PathParam; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping public class ProductController { @Autowired private ProductService productService; @PostConstruct public void createProducts(){ List<Product>products = new ArrayList<>(); for ( int i= 0 ;i< 100 ;i++){ Product product = new Product(); product.setPrice(i); product.setName( "product+" + i); products.add(product); } productService.saveAllProducts(products); } @GetMapping ( "/getAll/{offset}" ) public Iterable<Product> getAllProducts( @RequestParam Integer pageSize, @PathVariable ( "offset" ) Integer offset){ return productService.getAllProducts(pageSize,offset); } } |
Output:
The JSON Response from the backend will look like this

This response is then converted to the frontend according to in web, android, or ios.
Please Login to comment...