I’m trying to implement a reactive HATEOAS API with paging in WebFlux. I’m unsure whether this is feasible without sacrificing the reactive nature of the code.
First of all, Spring-Hateoas documentation on how to build links is currently a stub (“TODO”). This doesn’t raise confidence in whether this is possible, though I’m unsure why this would be. Can I not just use the LinkBuilder
from Spring MVC?
Then there is paging. I’ve seen some examples where they attempt to implement it like the folks at Baeldung, however, I find the code not as reactive to my liking:
@GetMapping("/products")
public Mono<Page<Product>> findAllProducts(Pageable pageable) {
return this.productRepository.findAllBy(pageable)
.collectList()
.zipWith(this.productRepository.count())
.map(p -> new PageImpl<>(p.getT1(), pageable, p.getT2()));
}
What doesn’t sit well with me is the usage of collectList()
. This seems to defeat the purpose of reactive programming if we’re buffering the whole result of the query before continuing.
The other, albeit quieter, crowd defends the position that paging shouldn’t be done in the same way as in imperative programming. I’ve read, e.g., in the answer of mp911de of this question that cursor-based pagination is more suitable.
My questions therefore are:
- What is the best practice when implementing HATEOAS with pagination
in a reactive context? - Is my perception of
collectList()
valid?