I'am trying to create a simple example of a CRUD with Spring Webflux, but I'am getting stucked in some concepts.
I know that I can return a Flux in my controller request mapping method. But sometimes I would like to return a 404 status, so I can handle in the front-end somehow.
I found a example in the official documentation to use 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 the return is a list (Flux) o persons, the ServerResponse.ok.body creates a Mono.
So my question: Is that the way it is? In other words, it does not matter if I have a Flux, does Spring always return a Mono of ServerResponse (or other similar class)?
For my 404 status I could use something like
.switchIfEmpty(ServerResponse.notFound().build());
But I was thinking in something more streaming way. That I could handle the list of objects element by element, for instance.
ServerResponse
is always a single element, hence a mono, what is returned inside thatServerResponse
can be a flux and that is also what should happen. – CowpokeFlux<ResponseEntity<Person>>
? – SeaddonFlux<>
, and define an exception and throw the exception as you needed, and handle the exception and set status code. – LanguedocFlux
and ignore error conditions (for example, directly passing on a MongoDB.findall()
) or layer onResponseEntity
and sometimesResponseWrapper
with little explanation. I just want one example that shows a real error-handling implementation that Spring would recommend. What is the best practice here? – Woollyheaded