Search code examples
springspring-bootspring-data-jpa

Map stream of Data into Page<T>


For quite a while now I am trying to get following to work in my resource / controller:

Have a stream of data and then map the stream of data to a Page response

Page<SomeType> controllerMethod() {

    List<SomeType> allItems = someRepository.findAll()
                              .filter(something)
                              .collect(Collectors.toList())

    return allItems.map(...?)
}

Question:

Are there any helpers from spring that can help me acheive this?

It would be ok that the paging is not done on DB level.


Solution

  • PageImpl has constructor from list

    Page<SomeType> controllerMethod() {
    
        List<SomeType> allItems = someRepository.findAll()
                                  .filter(something)
                                  .collect(Collectors.toList())
    
        return new PageImpl<>(allItems);
    }