How can I download a file using Swagger OpenAPI v3 implementation?
I am not able to figure out how to return the actual file content back in the response through my controller. Its literally straightforward without the use of OpenAPI, but here I must use the override method generated through the OpenAPI Interface.
When I hit the endpoint, the file is getting downloaded, but there is no content in that.
Swagger yaml file:
responses:
'200':
description: OK
content:
application/octet-stream:
schema:
type: string
format: base64
Spring Boot Controller:
@Override
public ResponseEntity<String> getFile(String storageIdentifier) throws IOException {
return ResponseEntity.ok().contentType(contentType(extension.get().toString()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
.body(new String(bytearray));
}
When I see the Swagger API documentation, it suggests to use
type: string and format: binary
(to return a file) or
type: string and format: base64
(to return a string)