【发布时间】:2019-01-14 18:35:18
【问题描述】:
如何使用 Spring 的反应式 WebClient 将 Flux<String> 作为 JSON 数组发布?
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();
这会发送请求,但不会将其作为 JSON 字符串数组发送。
我看到有另一个body() 签名接受ParameterizedTypeReference,所以我尝试了这个:
.body(stringFlux.collectList(), new ParameterizedTypeReference<>() {})
但这实际上会导致编译错误(我使用的是 Java 11):
Error:java: com.sun.tools.javac.code.Types$FunctionDescriptorLookupError.
有什么想法吗?
【问题讨论】:
标签: java spring-webflux project-reactor