-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid collectList when sending a Flux of objects as JSON using WebClient #28398
Comments
It is possible, but not with There is significant overhead for writing JSON content individually instead of collectively. That is why the Does that answer your question? |
Hey, I'm trying to POST to a server that only accepts
Which would then, atleast for what I know stream the objects to the target as they come available without having to wait for all of them. Would this kind of behaviour be possible to implement in WebClient or should I stick with more low level clients like reactor-netty? |
You can change the |
I see, however it does not produce valid JSON "out of the box". Heres what I tried: private final WebClient webClient = WebClient.builder()
.baseUrl("http://localhost:8080/")
.codecs(clientCodecConfigurer -> {
final Jackson2JsonEncoder jackson2JsonEncoder = new Jackson2JsonEncoder();
jackson2JsonEncoder.setStreamingMediaTypes(List.of(MediaType.APPLICATION_JSON));
clientCodecConfigurer.defaultCodecs().jackson2JsonEncoder(jackson2JsonEncoder);
})
.build();
Flux<MyEntity> flux = ...;
webClient.post()
.uri("/")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(flux, MyEntity.class)
.retrieve()
.bodyToMono(MyResponse.class) This will not wrap the JSON in an array nor add commas between the items. Should I manually map my |
Looking at the Javadoc of Maybe, if we are in the streaming section and the media type is "application/json" (i.e. explicitly set as a streaming media type), we could simply add the opening and closing square brackets to ensure valid JSON is produced. We could also switch to flushing at some regularity > 1 (or just leave it to the underlying server buffer) since we know it's a media type that implies continues writing and shouldn't require explicit flushes. In which case I'm even wondering about removing the non-streaming section entirely, and doing this by default, so that we always write |
@rstoyanchev is there a way to override this behavior to old-way now? |
@hu553in this issue is about the other way around: avoiding to buffer all elements before sending them as a single JSON document from the client. If you're using the server side of WebFlux and an |
Would it be possible to send a flux of objects in the request body using WebClient with "Content-Type: application/json" but avoid having to collect the whole flux as a list in memory?
Let's say I have a Flux containing millions of elements and my target server only accepts non-streaming content-types, the Jackson2JsonEncoder would then call "collectList()" on the flux, possibly running out of memory.
Couldn't the Jackson2JsonEncoder somehow write the objects as they come available?
The text was updated successfully, but these errors were encountered: