Monta
Monta

Reputation: 1180

Is it possible to pass Flux to the body of webClient POST?

I have an endpoint like this :

@PostMapping("/products")
Flux<Product> getProducts(@RequestBody Flux<String> ids) {
    return Flux...
}

On my client side, I want to consume this endpoint but not sure how to pass a Flux of String in the body (I don't want to make it a list)

Flux<Product> getProducts(Flux<String> ids) {
  return webClient.post().uri("/products")
      .body(/* .. how should I do here? ..*/)
      .retrieve()
      .bodyToFlux(Product.class);
}

Upvotes: 1

Views: 1456

Answers (1)

Michael McFadyen
Michael McFadyen

Reputation: 2987

You can, in fact, pass in a Flux into the .body() method on the WebClient

Flux<Person> personFlux = ... ;

Mono<Void> result = client.post()
        .uri("/persons/{id}", id)
        .contentType(MediaType.APPLICATION_STREAM_JSON)
        .body(personFlux, Person.class)
        .retrieve()
        .bodyToMono(Void.class);

Example taken from Spring Reference Docs

The variation of the body() method you'd want to use is:

<T,P extends org.reactivestreams.Publisher<T>> WebClient.RequestHeadersSpec<?> body(P publisher,
                                                                                    Class<T> elementClass)

Relevant JavaDoc for this method

Upvotes: 3

Related Questions