Pagination using Spring Boot Simple Example | JavaInUse

Pagination using Spring Boot Simple Example

In this post we expose a rest service which takes pageable parameters of page size and sort and return the data accordingly.


This tutorial is explained in the below Youtube Video.

Lets Begin-

We make use of H2 database. Also in the pom we have added the dependencies for spring-boot-devtools. This spring boot dependency provides us with a H2-DB UI interface. Late we will make of this UI to populate data in H2. Maven will be as follows-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


	<description>Demo project for Spring Boot</description>

		<relativePath /> <!-- lookup parent from repository -->










Create the as follows-
package com.javainuse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;

public class SpringBootHelloWorldApplication {

	public static void main(String[] args) {, args);

Create the Entity class as follows-
package com.javainuse.model;

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

public class Employee {
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	private String name;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getDept() {
		return dept;

	public void setDept(String dept) {
		this.dept = dept;

	private String dept;

	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", dept=" + dept + "]";


We define a RestController which maps the request /listPageable to the method employeesPageable. This method takes the Spring Pageable as parameter. Define the RestController as follows-
package com.javainuse.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.model.Employee;

public class EmployeeController {

	private EmployeeRepository employeeData;

	@RequestMapping(value = "/listPageable", method = RequestMethod.GET)
	Page<Employee> employeesPageable(Pageable pageable) {
		return employeeData.findAll(pageable);



Next we define the EmployeeRepository which is an interface that extends the Spring Framework class JpaRepository. JpaRepository class is a generics and takes the following two parameters as arguments-
  • What type of Object will this repository be working with- In our case Employee
  • Id will be what type of object- Long(since id defined in the Employee class is long)
Thats the only configuration required for the repository class. The required operations like adding and retrieving employee details from DB will now be handled.


import com.javainuse.model.Employee;

public interface EmployeeRepository extends JpaRepository<Employee
			, Long> {

Compile and the run the as a Java application.
We now use the H2-console to insert data in H2 database. Go to localhost:8080/h2-console/
In the JDBC url use jdbc:h2:mem:testdb. Keep the password blank.Click on Connect.
Now select the Employee table. Run the select query. Next add test data to the table.
Now go to http://localhost:8080/listPageable?page=0&size=3&sort=name

We can see only 3 elements are returned with sorting based on the name.

Download Source Code

Download it -
Spring Boot Pagination

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Hello World Application- Create simple controller and jsp view using Gradle Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions