Last active
February 8, 2021 13:04
-
-
Save ufuk/49cf74f6805b8c62f6ca44911f0526b3 to your computer and use it in GitHub Desktop.
SpringDataPageUtils to convert paged entities to paged model objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.data.domain.Page; | |
import org.springframework.data.support.PageableExecutionUtils; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
public final class SpringDataPageUtils { | |
public static <T, C> Page<C> convertPage(Page<T> page, Function<T, C> converter) { | |
return PageableExecutionUtils.getPage( | |
page.getContent() | |
.stream() | |
.map(converter) | |
.collect(Collectors.toList()), | |
page.getPageable(), | |
page::getTotalElements | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Page<Product> page = productRepository...; | |
Page<ProductModel> convertedPage = SpringDataPageUtils.convertPage(page, eachProduct -> { | |
ProductModel productModel = new ProductModel(); | |
productModel.setId(eachProduct.getId()); | |
... | |
return productModel; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment