Pagination in SpringBoot
Pagination is a crucial feature for web applications that deal with large datasets. It allows developers to split a big collection of data into smaller, more manageable chunks or pages. This not only enhances the user experience by reducing the time to load and display data but also minimizes the system's memory footprint as not all data needs to be loaded at once. Spring Boot simplifies the implementation of pagination with Spring Data JPA, which offers built-in support through the PagingAndSortingRepository interface.
Let's dive into how we can implement pagination in a Spring Boot application
Step 1: Include Dependencies
First, make sure you have the necessary dependencies in your pom.xml file if you are using Maven
<dependencies>
<dependency><groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
The h2 dependency is for the H2 database, which is an in-memory database perfect for demonstrations.
Step 2: Create a Domain Entity
Create a JPA entity that represents the data you wish to paginate. For example, a Book entity:
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String author;
//Don't forget to add getters and setters or may be use lombork for that
}
Step 3: Create a Repository Interface
Extend PagingAndSortingRepository to include pagination capabilities:
public interface BookRepository extends PagingAndSortingRepository<Book, Long> {
}
Step 4: Create a Service
Create a service layer that will use the repository to fetch paginated data:
@Service
public class BookService {
@Autowired
private BookRepository bookRepository;
public Page<Book> findPaginated(int pageNo, int pageSize) {
Pageable paging = PageRequest.of(pageNo, pageSize);
return bookRepository.findAll(paging);
}
}
Step 5: Create a REST Controller
Create a controller with an endpoint to return paginated results:
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping
public ResponseEntity<List<Book>> getAllBooks(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
Page<Book> pageResult = bookService.findPaginated(page, size);
return new ResponseEntity<>(pageResult.getContent(), HttpStatus.OK);
}
}
In the above controller, the getAllBooks endpoint accepts page and size parameters, which are used to fetch the paginated data.
Step 6: Test Your Application
Run your Spring Boot application and test the pagination:
<a href=localhost:8080/books?page=0&size=5 target="_blank" rel="noopener noreferrer">localhost:8080/books?page=0&size=5</a>
This will return the first five books. Adjusting the page and size request parameters will fetch different subsets of the data.