Reputation: 4511
I am working on a Rest API in my Spring boot application. Here response size is considerably large from 10 MB -100 MB.
Initially, I implemented the API as follows:
@GetMapping("/document/state")
public ResponseEntity<String> getLucidChartDocumentState(
@RequestParam(required = true, name = "documentId") String documentId) throws IOException {
return ResponseEntity.ok()
.body(lucidChartDocumentService.getDocumentState(documentId).getBody());
}
I am not sure if returning the response as a Response is a good approach.
Upvotes: 0
Views: 3141
Reputation: 12932
Ideally you wouldn't load content of the file into a memory or you'll quickly run out of it even with relatively load traffic.
If your service class returns an InputStream
you can wrap it into InputStreamResource and return in a response body. Note: you should have way to get the content length of the object without reading it to byte array.
InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
httpHeaders.setContentLength(contentLength);
return new ResponseEntity(inputStreamResource, httpHeaders, HttpStatus.OK);
Upvotes: 1