Home > Enterprise >  Spring Boot, download CSV File, Swagger Produces Media Type
Spring Boot, download CSV File, Swagger Produces Media Type

Time:10-03

I am writing a Spring Boot SwaggerOpen API that will download/get a CSV File Template. What should be the produces = MediaType ? I cannot find CSV below.

enter image description here

CodePudding user response:

You can enter the string directly, without using MediaType. For example: produces = "text/csv". text/plain should work just fine though, since a csv file is just comma seperated text.

CodePudding user response:

octet-stream can be also used whenever you are returning file as byte array (ResponseEntity<byte[]>) from your API. Also as @Orfeas mentioned you don't have to use values from MediaType. Also produces are not required so you don't have to specify it. in this case swagger generated will looks like this:

responses:
        200:
          description: "Data was successfully sent"
          schema:
            type: "array"
            items:
              type: "string"
              format: "byte"

On the ui side if you are using javascript f.e. or any javascript framework you can use Blob type to process response from the server.

Maybe there is better solution for it but I usually using this one.

  • Related