1

My application is build on SprinBoot, Java Technology I want write API for file download and I am using swagger-codegen-maven-plugin and in the specification its mentioned as below for file download

content:
    application/octect-stream:
        schema:
            type: string
            format: binary

The Code which is auto generated via swagger codegen contains the return type as ResponseEntity<String> in java SpringBoot application. Now I have file data in byte[], how to convert it to String and send as application/octect-stream ?.

Also let me know if my data is in File object insteam of byte[] how to send it ?

aeroboy
  • 149
  • 4
  • 14

1 Answers1

0

If your api is supposed to return a file which must be downloaded as response of it then your api must return responseResponseEntity. Below is the snapshot of the code you can try.

private ResponseEntity<ByteArrayResource> getRespEntity(Byte[] fileByte) {
    ByteArrayResource resource = new ByteArrayResource(fileByte);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    headers.add("Pragma", "no-cache");
    headers.add("Expires", "0");
    
    return ResponseEntity.ok().headers(headers).contentLength(resource.contentLength())
            .contentType(MediaType.parseMediaType("application/octet-stream"))
            .body(resource);
}   
P.Sanjay
  • 303
  • 1
  • 7