In my application (Spring MVC) I have a legacy endpoint returning a ResponseEntity<StreamingResponseBody>
. The StreamingResponseBody
is basically a ZipOutputStream
built by fetching a list of documents in a S3 storage.
Omitting some parts, the code is as below:
@GetMapping(value = "/zip", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
ResponseEntity<StreamingResponseBody> downloadZip() {
List<String> keys = getMyKeys();
StreamingResponseBody streamRespBody = outputStream -> {
try {
// In getZipOutputStream() each document is fetched from S3 directly into the
// outputStream (connected to the client browser through
// the StreamingResponseBody) so we don't put everything in memory
this.documentService.getZipOutputStream(outputStream, keys);
} catch (S3Exception e) {
throw new RuntimeException(e);
}
};
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=documents.zip")
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(streamRespBody);
}
Now I have to write something equivalent using WebFlux. I have looked around various documentations, resources, tutorials... but I can't figure a working example.
Is is possible to build something equivalent possible with WebFlux, streaming a Zip to a client browser ?
For now, I can't even figure if I should return something like a Flux<SomeClasse>
or if my endpoint should be a Mono<void> getZip(HttpServerResponse httpResponse)
and write the ZipOutputStream
to the client through the httpResponse.