How do I use Spring's reactive WebClient to POST a Flux<String> as a JSON array?
Flux<String> stringFlux = Flux.fromIterable(objects).map(MyObject::getSomeString);
WebClient.create(baseUrl)
.post()
.uri(myUrl)
.contentType(MediaType.APPLICATION_JSON)
.body(stringFlux, String.class)
.exchange()
.flatMap(response -> {
if (response.statusCode().is2xxSuccessful()) {
// Do something
}
return response.bodyToMono(Void.class);
})
.block();
This sends the request, but it's not sending it as a JSON array of strings.
I saw that there's another body() signature that accepts a ParameterizedTypeReference, so I tried this:
.body(stringFlux.collectList(), new ParameterizedTypeReference<>() {})
but this results in a compile error actually (I'm on Java 11):
Error:java: com.sun.tools.javac.code.Types$FunctionDescriptorLookupError.
Any ideas?