cviniciusm
cviniciusm

Reputation: 139

How to get Person instead of Mono<Person>?

On the retrieve code below, how to get Person instead of Mono or how to get Person from Mono, please ?

23.2.3 Request and Response Body Conversion

The response body can be one of the following:

Account — serialize without blocking the given Account; implies a synchronous, non-blocking controller method.

1.7.1. Retrieve

WebClient client = WebClient.create("http://example.org");

Mono<Person> result = client.get()
                      .uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
                      .retrieve()
                      .bodyToMono(Person.class);

Upvotes: 2

Views: 1053

Answers (1)

Brian Clozel
Brian Clozel

Reputation: 59211

Once you have a Mono<Person> instance available, you have two choices:

  1. compose that reactive type (i.e. use operators available on that type) and use it to save that data in a datastore, serve it as a HTTP response body, etc
  2. or call Person person = result.block() on it, which blocks. So you should not do that in a reactive application because this might completely block the few threads available to your application.

Upvotes: 1

Related Questions