Course – Black Friday 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Partner – Orkes – NPI EA (cat=Java)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2025 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll see how we can convert a List<Object> into a Page<Object> using Spring Data JPAIn Spring Data JPA applications, it’s common to retrieve data from a database in a pageable manner. However, there may be situations where we need to convert a list of entities into a Page object for use in a pageable endpoint. For example, we might want to retrieve data from an external API or process data in memory.

We’ll set a simple example to help us visualize the data flow and conversion. We’ll divide our system into RestController, Service, and Repository layers and see how we can work to convert a large set of data retrieved from the database as List<Object> into smaller, more organized pages using the pagination abstractions provided by Spring Data JPA. Finally, we’ll write some tests to observe the pagination in action.

2. Key Pagination Abstractions in Spring Data JPA

Let’s have a brief look at the key abstractions provided by Spring Data JPA that are used in producing paginated data.

2.1. Page

Page is one of the key interfaces provided by Spring Data to facilitate pagination. It provides a way to represent and manage large result sets returned from database queries in a paginated format.

We can then use the Page object to display the required number of records to the user and links to navigate to subsequent pages.

Page encapsulates details such as the content of the page, along with metadata involving pagination details such as page number and page size, whether there is a next or previous page, how many elements are remaining, and the total number of pages and elements.

2.2. Pageable

Pageable is an abstract interface for pagination information. The concrete class that implements this interface is PageRequest. It represents the pagination metadata, such as the current page number, the number of elements per page, and the sorting criteriaIt’s an interface in Spring Data JPA that provides a convenient way to specify pagination information for a query or, in our case, to bundle the paging information together with the contents to create the Page<Object> from a List<Object>.

2.3. PageImpl

Finally, there’s the PageImpl class, which provides a convenient implementation of the Page interface that can be used to represent a page of results from a query, including the pagination metadata. It’s commonly used in conjunction with Spring Data’s repository interfaces and pagination mechanisms to retrieve and manipulate data in a pageable manner.

Now that we’ve got a basic understanding of the involved components, let’s set up a simple example.

3. Example Setup

Let’s consider a simple example of a customer information microservice, with a REST endpoint that can get paginated customer data based on the request parameters. We’ll set up the required dependencies first in the POM. The required dependencies are spring-boot-starter-data-jpa and spring-boot-starter-web, and we’ll also add spring-boot-starter-test for testing purposes:

<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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependendencies>

Next. let’s set up the REST Controller.

3.1. Customer Controller

First off, let’s add the appropriate method with the request parameters that will drive our logic in the service layer:

@GetMapping("/api/customers")
public ResponseEntity<Page<Customer>> getCustomers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {

    Page<Customer> customerPage = customerService.getCustomers(page, size);
    HttpHeaders headers = new HttpHeaders();
    headers.add("X-Page-Number", String.valueOf(customerPage.getNumber()));
    headers.add("X-Page-Size", String.valueOf(customerPage.getSize()));

    return ResponseEntity.ok()
      .headers(headers)
      .body(customerPage);
}

Here, we can see that the getCustomers method is expecting a ResponseEntity of type Page<Customer>.

3.2. Customer Service

Next, let’s set up our service class that will interact with the repository, convert the data into the required Page, and return it to the Controller:

public Page<Customer> getCustomers(int page, int size) {

    List<Customer> allCustomers = customerRepository.findAll();
//... logic to convert the List<Customer> to Page<Customer>
//... return Page<Customer>
}

Here, we’ve omitted the details, just focusing on the fact that the service calls the JPA repository and is getting a potentially large set of Customer data as List<Customer>. Next, let’s look into the details of how we can use the JPA-provided API to convert this list into Page<Customer>.

4. Converting List<Customer> to Page<Customer>

Now, let’s furnish the details of how the CustomerService can convert the List<Customer> received from CustomerRepository into a Page object. Essentially, after retrieving a list of all customers from the database, we want to create a Pageable object using the PageRequest factory method:

private Pageable createPageRequestUsing(int page, int size) {
    return PageRequest.of(page, size);
}

Note that these page and size parameters are the ones passed down as request parameters from our CustomerRestController to CustomerService.

We’ll then split the large list of Customer into a sub-list. We need to know the start and end indices, based on which we can create a sub-listThis can be calculated using the getOffset() and getPageSize() methods of the Pageable object:

int start = (int) pageRequest.getOffset();

Next, let’s get the end index:

int end = Math.min((start + pageRequest.getPageSize()), allCustomers.size());

This sublist will form the content of our Page object:

 List<Customer> pageContent = allCustomers.subList(start, end);

Finally, we’ll create an instance of PageImpl. It will encapsulate the pageContent as well as the pageRequest  along with the total size of the List<Customer>:

new PageImpl<>(pageContent, pageRequest, allCustomers.size());

Let’s put all the pieces together in one place:

public Page<Customer> getCustomers(int page, int size) {

    Pageable pageRequest = createPageRequestUsing(page, size);

    List<Customer> allCustomers = customerRepository.findAll();
    int start = (int) pageRequest.getOffset();
    int end = Math.min((start + pageRequest.getPageSize()), allCustomers.size());

    List<Customer> pageContent = allCustomers.subList(start, end);
    return new PageImpl<>(pageContent, pageRequest, allCustomers.size());
}

5. Converting Page<Customer> to List<Customer>

Pagination is an important feature to consider when working with a large result set. It can significantly enhance the data organization by breaking it down into smaller parts called pages.

Spring data provides a convenient way to support pagination. To add paging in a query method, we need to change the signature to accept a Pageable object as an argument and return Page<T> instead of List<T>.

Typically, the returned Page object denotes a portion of the result. It holds information about the list of elements, the number of all elements, and the number of pages.

For instance, let’s see how we get the list of customers of a given page:

public List<Customer> getCustomerListFromPage(int page, int size) {
    Pageable pageRequest = createPageRequestUsing(page, size);
    Page<Customer> allCustomers = customerRepository.findAll(pageRequest);

    return allCustomers.hasContent() ? allCustomers.getContent() : Collections.emptyList();
}

As we can see, we used the same createPageRequestUsing() factory method to create a Pageable object. Then, we called the getContent() method to get the returned page as a list of customers.

Please note that we used the hasContent() method to check whether the page has content before calling getContent().

6. Testing the Service

Let’s write a quick test to see that the List<Customer> is split into Page<Customer> and that the page size and page number are correct. We’ll mock the customerRepository.findAll() method to return the list of Customer having size 20.

In the setup, we simply provide this list when findAll() is called:

@BeforeEach
void setup() {
    when(customerRepository.findAll()).thenReturn(ALL_CUSTOMERS);
}

Here, we’re constructing a parameterized test and asserting on the contents, content size, total elements, and total pages:

@ParameterizedTest
@MethodSource("testIO")
void givenAListOfCustomers_whenGetCustomers_thenReturnsDesiredDataAlongWithPagingInformation(int page, int size, List<String> expectedNames, long expectedTotalElements, long expectedTotalPages) {
    Page<Customer> customers = customerService.getCustomers(page, size);
    List<String> names = customers.getContent()
      .stream()
      .map(Customer::getName)
      .collect(Collectors.toList());

    assertEquals(expectedNames.size(), names.size());
    assertEquals(expectedNames, names);
    assertEquals(expectedTotalElements, customers.getTotalElements());
    assertEquals(expectedTotalPages, customers.getTotalPages());}

Finally, the test data input and output for this parametrized test are:

private static Collection<Object[]> testIO() {
    return Arrays.asList(
      new Object[][] {
        { 0, 5, PAGE_1_CONTENTS, 20L, 4L },
        { 1, 5, PAGE_2_CONTENTS, 20L, 4L },
        { 2, 5, PAGE_3_CONTENTS, 20L, 4L },
        { 3, 5, PAGE_4_CONTENTS, 20L, 4L },
        { 4, 5, EMPTY_PAGE, 20L, 4L } }
    );
}

Each test runs the service method with a different pair of page sizes (0,1,2,3,4), and each is expected to have 5 elements. We expect the total number of pages to be 4 since the total size of the original list is 20. Lastly, each page is expected to contain 5 elements.

Now, we’re going to add test cases for converting Page<Customer> to List<Customer>. First, let’s test the scenario where the Page object is not empty:

@Test
void givenAPageOfCustomers_whenGetCustomerList_thenReturnsList() {
    Page<Customer> pagedResponse = new PageImpl<Customer>(ALL_CUSTOMERS.subList(0, 5));
    when(customerRepository.findAll(any(Pageable.class))).thenReturn(pagedResponse);

    List<Customer> customers = customerService.getCustomerListFromPage(0, 5);
    List<String> customerNames = customers.stream()
      .map(Customer::getName)
      .collect(Collectors.toList());

    assertEquals(PAGE_1_CONTENTS.size(), customers.size());
    assertEquals(PAGE_1_CONTENTS, customerNames);
}

Here, we mocked the invocation of the customerRepository.findAll(pageRequest) method to return a Page object holding a portion of the ALL_CUSTOMERS list. As a result, the returned customer list is the same as the wrapped one in the given Page object.

Next, let’s see what happens if customerRepository.findAll(pageRequest) returns an empty page:

@Test
void givenAnEmptyPageOfCustomers_whenGetCustomerList_thenReturnsEmptyList() {
    Page<Customer> emptyPage = Page.empty();
    when(customerRepository.findAll(any(Pageable.class))).thenReturn(emptyPage);
    List<Customer> customers = customerService.getCustomerListFromPage(0, 5);

    assertThat(customers).isEmpty();
}

Unsurprisingly, the returned list is empty.

7. Testing the Controller

Finally, let’s also test the Controller to make sure we’re getting the ResponseEntity<Page<Customer>> back as JSON. We’ll use MockMVC to send a request to the GET endpoint and expect the paged response with the expected parameters:

@Test
void givenTotalCustomers20_whenGetRequestWithPageAndSize_thenPagedReponseIsReturnedFromDesiredPageAndSize() throws Exception {

    MvcResult result = mockMvc.perform(get("/api/customers?page=1&size=5"))
      .andExpect(status().isOk())
      .andReturn();

    MockHttpServletResponse response = result.getResponse();

    JSONObject jsonObject = new JSONObject(response.getContentAsString());
    assertThat(jsonObject.get("totalPages")).isEqualTo(4);
    assertThat(jsonObject.get("totalElements")).isEqualTo(20);
    assertThat(jsonObject.get("number")).isEqualTo(1);
    assertThat(jsonObject.get("size")).isEqualTo(5);
    assertThat(jsonObject.get("content")).isNotNull();}

Essentially, we are using a MockMvc instance to simulate an HTTP GET request to the /api/customers endpoint. We’re providing the query parameters page=1 and size=5. We then expect a successful response and a body containing page metadata and content.

Finally, let’s take a quick look at how converting a List<Customer> to Page<Customer> can benefit the API design and consumption.

8. Benefits of Using Page<Customer> Over List<Customer>

Choosing to return Page<Customer> over the entire List<Customer> as an API response in our example can have some benefits depending upon the use case. One of the benefits is optimized network traffic and processing. Essentially here, if the underlying data source returns a large list of customers, converting it to a Page<Customer> allows the client to request only a specific page of results, rather than the entire list. This can lead to simplified processing at the client and reduce network load.

Additionally, by returning a Page<Customer>, the API provides a standardized response format that clients can easily understand and consume. The Page<Customer> object contains the list of customers for the requested page and metadata such as the total number of pages and the number of items per page.

Finally, converting the list of objects to a page provides flexibility in the API design. For example, the API can allow clients to sort the results by different fields. We can also filter the results based on criteria, or even return a subset of fields for each customer.

9. Conclusion

In this tutorial, we used Spring Data JPA to handle conversion between a List<Object> and a Page<Object>. We used the API provided by Spring Data JPA including Page, Pageable, and PageImpl classes. Finally, we briefly looked into some of the benefits of using Page<Object> over List<Object>.

In summary, converting a List<Object> to a Page<Object> in a REST endpoint provides a more efficient, standardized, and flexible way to handle large datasets and implement pagination in the API.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Course – Black Friday 2025 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2025 – NPI (All)
announcement - icon

Yes, we're now running our Black Friday Sale. All Access and Pro are 33% off until 2nd December, 2025:

>> EXPLORE ACCESS NOW

eBook Jackson – NPI EA – 3 (cat = Jackson)