I dont see any major time complexity difference between these two approaches, both of them working charm, I would like to understand what is the major different between these two approach
I'm getting collection of Student object from service.
bodyToMono ParameterizedTypeReference
public Mono<Collection<Student>> getStudents(String id) {
return webClient
.get()
.uri(uriBuilder -> uriBuilder
.path("/students/{0}")
.build(id))
.retrieve()
.onStatus(HttpStatus::isError, resp -> resp.createException()
.map(WebClientGraphqlException::new)
.flatMap(Mono::error)
).bodyToMono(new ParameterizedTypeReference<Collection<Student>>() {}); // This Line
}
bodyToFlux Collectors
public Mono<Collection<Student>> getStudents(String id) {
return webClient
.get()
.uri(uriBuilder -> uriBuilder
.path("/students/{0}")
.build(id))
.retrieve()
.onStatus(HttpStatus::isError, resp -> resp.createException()
.map(WebClientGraphqlException::new)
.flatMap(Mono::error)
).bodyToFlux(Student.class).collect(Collectors.toList()); // This Line
}
The method bodyToMono() extracts the body to a Mono instance. The method Mono. block() subscribes to this Mono instance and blocks until the response is received.
WebClient.ResponseSpec. onStatus(Predicate<HttpStatus> statusPredicate, Function<ClientResponse,reactor.core.publisher.Mono<? extends Throwable>> exceptionFunction) Provide a function to map specific error status codes to an error signal to be propagated downstream instead of the response.
If you are retrieving a single item, use bodyToMono. It emits 0-1 items
For multiple items, use bodyToFlux. It emits 0-N items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With