Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between bodyToMono ParameterizedTypeReference and bodyToFlux

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
  }
like image 363
RenceAbi Avatar asked May 15 '20 07:05

RenceAbi


People also ask

What is bodyToMono?

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.

What is WebClient ResponseSpec?

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.


1 Answers

  • If you are retrieving a single item, use bodyToMono. It emits 0-1 items

  • For multiple items, use bodyToFlux. It emits 0-N items.

like image 135
Colon Avatar answered Oct 29 '22 14:10

Colon