How to serve files/PDF files the reactive way in spring
The easiest way to achieve that would be with a Resource
.
@GetMapping(path = "/pdf", produces = "application/pdf")
ResponseEntity<Resource> getPDF() {
Resource pdfFile = ...;
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData(file.getName(), file.getName());
return ResponseEntity
.ok().cacheControl(CacheControl.noCache())
.headers(headers).body(resource);
}
Note that DataBufferUtils
has some useful methods there that convert an InputStream
to a Flux<DataBuffer>
, like DataBufferUtils#read()
. But dealing with a Resource
is still superior.