I'm supposed to download a ZIP file from 3rd party API. However, when I'm using Spring Webclient I end up with an empty file. Any ideas what I'm doing wrong? What is the proper way to store the content of Flux<DataBuffer>
to a file?
Here's my code:
WebClient client = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient1))
.baseUrl(String.format(FILE_DOWNLOAD_PATH))
.build();
WebClient.RequestBodyUriSpec method = client.method(HttpMethod.GET);
method.header("Authorization", "Bearer " + accessToken);
final Flux<DataBuffer> dataBufferFlux = method.retrieve().bodyToFlux(DataBuffer.class);
final Path path = FileSystems.getDefault().getPath("example" + new Random(200).nextInt() + ".zip");
WritableByteChannel channel = Files.newByteChannel(path, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
Mono<Path> then = DataBufferUtils.write(dataBufferFlux, channel)
.map(DataBufferUtils::release)
.then(Mono.just(path));
UPDATE: The above code is being called directly from main, and then the application shuts down. Could this be an issue?
block()
on yourthen
variable. – Contemporary