今回は、ListからPageに変換する方法についてメモしていきたいと思います。
ListからPage変換
変換するには「PageImpl」クラスを使います。コンストラクタは2つあって以下となります。
public PageImpl(List<T> content, Pageable pageable, long total)
public PageImpl(List<T> content)
今回は単純にListとからPageへの変換なので2番目のコンストラクタを使用し、変換を行います。
public Page<BoardDTO> findAll() {
return new PageImpl<>(
boardRepository.findAll(PageRequest.of(0, 20)).stream()
.map(BoardDTO::new)
.collect(toList()));
}
