0
0
SpringbootHow-ToBeginner · 4 min read

How to Implement Pagination in Spring Boot Easily

In Spring Boot, implement pagination by using the Pageable interface in your repository methods and returning a Page object. This allows you to request specific pages of data with size and sorting options easily.
📐

Syntax

Use Pageable as a parameter in repository methods to request a page of data. The method returns a Page<Entity> which contains the data and pagination info.

  • Pageable: Defines page number, size, and sorting.
  • Page<T>: Holds the content and metadata like total pages.
  • Repository method example: Page<User> findAll(Pageable pageable);
java
public interface UserRepository extends JpaRepository<User, Long> {
    Page<User> findAll(Pageable pageable);
}
💻

Example

This example shows a Spring Boot REST controller that returns paginated users. The client can specify page number and size via query parameters.

java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/users")
    public Page<User> getUsers(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "5") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return userRepository.findAll(pageable);
    }
}

// User entity and UserRepository are assumed to be defined properly.
Output
{ "content": [ {"id":1,"name":"Alice"}, {"id":2,"name":"Bob"}, {"id":3,"name":"Carol"}, {"id":4,"name":"David"}, {"id":5,"name":"Eve"} ], "pageable": { "pageNumber": 0, "pageSize": 5 }, "totalPages": 4, "totalElements": 20 }
⚠️

Common Pitfalls

Common mistakes include:

  • Not passing Pageable to repository methods, which disables pagination.
  • Using List<T> instead of Page<T>, losing pagination metadata.
  • Ignoring zero-based page indexing (page 0 is the first page).
  • Not validating page and size parameters, which can cause errors or unexpected results.
java
/* Wrong: returns all users without pagination metadata */
List<User> findAll();

/* Right: returns paginated users with metadata */
Page<User> findAll(Pageable pageable);
📊

Quick Reference

Tips for smooth pagination in Spring Boot:

  • Use Pageable in repository methods.
  • Return Page<T> to get content and page info.
  • Use PageRequest.of(page, size) to create pageable objects.
  • Remember page index starts at 0.
  • Validate input parameters to avoid errors.

Key Takeaways

Use Pageable in repository methods to enable pagination.
Return Page to access both data and pagination details.
Page numbering starts at 0, not 1.
Validate page and size parameters from clients.
Use PageRequest.of(page, size) to create Pageable instances.