It seems like it the Spring RestTemplate
isn\'t able to stream a response directly to file without buffering it all in memory. What is the proper to achieve this us
I cannot test whether or not the following code effectively does not buffer the contents of webClient
payload in memory. Nevertheless, i think you should start from there:
public Mono testWebClientStreaming() throws IOException {
Flux stream =
webClient
.get().accept(MediaType.APPLICATION_OCTET_STREAM)
.retrieve()
.bodyToFlux(DataBuffer.class);
Path filePath = Paths.get("filename");
AsynchronousFileChannel asynchronousFileChannel = AsynchronousFileChannel.open(filePath, WRITE);
return DataBufferUtils.write(stream, asynchronousFileChannel)
.doOnNext(DataBufferUtils.releaseConsumer())
.doAfterTerminate(() -> {
try {
asynchronousFileChannel.close();
} catch (IOException ignored) { }
}).then();
}