Using spring controller, endpoint returns file in the body response. I want to be sure to not to get resource leaks using "try with resources", but in that way in postman I get an error:
"error": "Internal Server Error", "message": "Stream Closed",
snippet of code in Spring controller:
InputStreamResource result;
ResponseEntity<Resource> response;
try(FileInputStream ios = new FileInputStream(file)){
result = new InputStreamResource(ios);
response = ResponseEntity.ok()
.headers(/*some headers here*/)
.contentLength(file.length())
.contentType(/*some media type here*/)
.body(result);
logger.info("successfully created");
return response;
} catch (IOException e) {
//some code here..
}
Interesting, that in logs I got success message, but in postman or in browser (this is a GET request) I got an error.
And it would work, if not to use "try-with-resource", but I'm afraid of resource leaks in that way.