Is there a way to implement pagination in spring webflux and spring data reactive

I'm trying to understand the reactive part of spring 5. I created a simple rest endpoint to search for all objects using spring web-fluxand spring reactive (mongo) data, but I see no way how to implement pagination.

Here is my simple example in Kotlin:

@GetMapping("/posts/")
fun getAllPosts() = postRepository.findAll()

Does this mean that a reactive endpoint does not require pagination? Is there a way to implement pagination from the server using this stack?

+4
source share
2 answers

Spring Page. Pageable , limit offset , , , Flux<T>, .

Flux<Person> findByFirstname(String firstname, Pageable pageable);

Reference Documentation 2.0.RC2 Spring .

+6

Flux skip take , filter sort .

.

@GetMapping("")
public Flux<Post> all(@RequestParam(value = "q", required = false) String q,
                      @RequestParam(value = "page", defaultValue = "0") long page,
                      @RequestParam(value = "size", defaultValue = "10") long size) {
    return this.postRepository.findAll()
        .filter(p -> Optional.ofNullable(q).map(key -> p.getTitle().contains(key) || p.getContent().contains(key)).orElse(true))
        .sort(comparing(Post::getCreatedDate).reversed())
        .skip(page * size).take(size);
}
0

Source: https://habr.com/ru/post/1684624/


All Articles