What is the difference between bodyToMono ParameterizedTypeReference and bodyToFlux

Viewed 8377

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
  }
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.

more about handler function spring reactive web handler

Related