Returning list of items using Spring Webflux

I am trying to create a simple CRUD example with Spring Webflux but I am stuck with some concepts.

I know that I can return Flux to my controller request mapping method. But sometimes I would like to return a 404 status, so I can somehow deal with the interface.

I found an example in the official documentation for using the ServerResponse object:

        public Mono<ServerResponse> listPeople(ServerRequest request) { 
                Flux<Person> people = repository.allPeople();
                return ServerResponse.ok().contentType(APPLICATION_JSON).body(people, Person.class);
        }

      

As you can see, even when a (Flux) o list is returned, ServerResponse.ok.body creates mono.

So my question is, is it so? In other words, it doesn't matter if I have Flux, Spring always returns Mono ServerResponse (or some other similar class)?

For my 404 status, I could use something like

.switchIfEmpty(ServerResponse.notFound().build());

      

But I was thinking about something more streaming. For example, I could process a list of element by element.

+3


source to share





All Articles