본문 바로가기
Language/Java

자바 List<class>를 Page로 Return 하는 방법

by wakestand 2022. 8. 9.
반응형
List<SampleDto> sampleDtoList = sampleRepository.findAll().stream()
        .map(model -> new SampleDto().fromEntity(model)).collect(Collectors.toList())

 

자바에서 List<Class> 형태의 sampleDtoList를

page로 return을 받고 싶은 경우

 

return new PageImpl<>(treasurySummeryDtoList);

 

위와 같이 new PageImpl<>(List명);

을 사용해주면

 

바로 List에서 Page로 변환이 가능하고

응용을 해 보자면

 

  public Page<SampleDto> getSampleByPageable(Pageable pageable) {
    return new PageImpl<>(sampleRepository.getSample(pageable).stream()
        .map(model -> new SampleDto().fromEntity(model)).collect(Collectors.toList()));
  }

 

List를 바로 PageImpl 생성 시

파라미터로 넣어주면 끝이다

반응형

댓글