This lesson focuses on implementing pagination in a Spring Boot application, starting with writing a test to fetch CashCards one at a time, sorted from highest to lowest amount (descending order).
Key Steps and Concepts:
Writing the Pagination Test:
shouldReturnAPageOfCashCards
is added to CashCardApplicationTests
./cashcards?page=0&size=1
, expecting to retrieve one CashCard per page.Running the Tests and Initial Failure:
Upon running the tests, the assertion fails because all three CashCards are returned instead of just one.
expected: 1
but was: 3
This failure is expected since pagination has not been implemented yet in the controller.
Implementing Pagination in the Controller:
A new findAll(Pageable pageable)
method is added to CashCardController
.
@GetMapping
private ResponseEntity<List<CashCard>> findAll(Pageable pageable) {
Page<CashCard> page = cashCardRepository.findAll(
PageRequest.of(
pageable.getPageNumber(),
pageable.getPageSize()
));
return ResponseEntity.ok(page.getContent());
}
The Pageable
object automatically captures the page
and size
parameters from the query string.
Understanding the Pagination Code:
Pageable
is provided by Spring Web to handle pagination parameters.PageRequest.of()
creates a PageRequest
object using the page number and size.PageRequest
is a simple implementation of Pageable
used for pagination.Compiling and New Compilation Error:
Running the tests now results in a compilation error:
error: method findAll in interface CrudRepository<T,ID> cannot be applied to given types;
This happens because CrudRepository
's findAll()
method does not support pagination parameters.
Extending PagingAndSortingRepository
:
To fix the error, CashCardRepository
is updated to extend PagingAndSortingRepository
in addition to CrudRepository
:
import org.springframework.data.repository.PagingAndSortingRepository;
...
interface CashCardRepository extends CrudRepository<CashCard, Long>, PagingAndSortingRepository<CashCard, Long> { ... }
This adds support for pagination and sorting in the repository.
Encountering a Runtime Error:
After making the repository changes, running the tests leads to a runtime error:
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'cashCardController' method
The error indicates that there are two methods mapped to the same endpoint /cashcards
with the same HTTP method.
Resolving the Ambiguous Mapping:
The issue is caused by having two @GetMapping
methods in CashCardController
mapped to /cashcards
:
findAll(Pageable pageable)
method.findAll()
method without pagination.To resolve the conflict, the old findAll()
method is removed:
// Delete this one:
@GetMapping()
private ResponseEntity<Iterable<CashCard>> findAll() {
return ResponseEntity.ok(cashCardRepository.findAll());
}
Rerunning the Tests:
Conclusion:
Pageable
and PageRequest
.PagingAndSortingRepository
.한국어 버전:
페이징 구현
이번 강의에서는 Spring Boot 애플리케이션에서 페이징을 구현하는 방법에 대해 알아봅니다. 먼저 테스트를 작성하여 한 번에 하나의 CashCard를 가져오고, 금액을 높은 순서로 정렬하는 작업부터 시작합니다.
주요 단계 및 개념:
CashCardApplicationTests
에 새로운 테스트 메소드 shouldReturnAPageOfCashCards
를 추가합니다./cashcards?page=0&size=1
로 GET 요청을 보내 한 페이지에 하나의 CashCard만 가져옵니다.