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.

Video

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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javainuse</groupId>
	<artifactId>SpringBootHelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

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

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
	
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
		</dependency>

		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>


	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>
	
Create the SpringBootHelloWorldApplication.java 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;

@RestController
@EnableAutoConfiguration
@SpringBootApplication
public class SpringBootHelloWorldApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootHelloWorldApplication.class, 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;

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

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDept() {
		return dept;
	}

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

	private String dept;

	@Override
	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.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.data.EmployeeRepository;
import com.javainuse.model.Employee;

@RestController
public class EmployeeController {

	@Autowired
	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.
package com.javainuse.data;

import org.springframework.data.jpa.repository.JpaRepository;

import com.javainuse.model.Employee;

public interface EmployeeRepository extends JpaRepository<Employee
			, Long> {

}
Compile and the run the SpringBootHelloWorldApplication.java as a Java application.
We now use the H2-console to insert data in H2 database. Go to localhost:8080/h2-console/login.do.
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