Part 6 - Pagination

  • To optimize performance, it is important to limit the amount of data returned, especially in the case of a mobile client.
  • REST services have the ability to give clients access large datasets in manageable chunks, by splitting the data into discrete pages or paging data.
  • For this lab, we will approach this by implementing the page number pagination pattern.

-

Get Data From Page

  • For example, a client wanting a blog post in page 3 of a hypothetical blog service can use a GET method resembling the following: http://blog.example.com/posts?page=3

-

Limit Data Retrieved From Page

  • It is possible for the client to override the default page size by passing in a page-size parameter: http://blog.example.com/posts?page=3&size=20

-

Pagination Data

  • Pagination-specific information includes
    • total number of records
    • total number of pages
    • current page number
    • page size
  • In the above scenario, one would expect a response body with pagination information closely resembling the JSON object below.
{
"data": [
         ... Blog Data
    ],
    "totalPages": 9,
    "currentPageNumber": 2,
    "pageSize": 10,
    "totalRecords": 90
}
  • Read more about REST pagination in Spring by clicking here.

-

Part 6.1 - Load Dummy Poll Data

  • Create a src/main/resource/import.sql file with DML statements for populating the database upon bootstrap. The import.sql should insert at least 15 polls, each with 3 or more options.

    • Below is an example of SQL statements for creating a single poll with only one option.

      • Poll Creation

        insert into poll (poll_id, question) values (1, 'What is your favorite color?');
        
      • Option Creation

        insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
        
  • Restart your application.

  • Use Postman to ensure database is populated by import.sql.

Part 6.2 - Spring's Built-in Pagination

  • Make use of Spring's built-in page number pagination support by researching org.springframework.data.repository.PagingAndSortingRepository.
  • Modify respective Controller methods to handle Pageable arguments.
  • Send a GET request to http://localhost:8080/polls?page=0&size=2 via Postman.
  • Ensure the response is a JSON object with pagination-specific information.