简体   繁体   中英

What is the right way to return a binary PDF from Springboot REST resources with Swagger/OpenAPI annotations?

How do I define a REST endpoint that returns a PDF binary using SpringBoot REST in Java with Swagger annotations?

A lot of answers to similar questions use @RequestMapping, but I am using Swagger/OpenAPI annotations like @ApiResponse. On my resource method, should I indicate the content type with 2 annotations?

@ApiResponse(content = @Content(mediaType = "application/pdf", schema=@Schema(type="string", format="binary"))) 
@Produces("application/pdf")

When I try this and return a ResponseEntity<byte[]> or ResponseEntity< ByteArrayStream > like this:

return ResponseEntity.ok()
          .contentType(org.springframework.http.MediaType.APPLICATION_PDF)
          .cacheControl(CacheControl.noCache())
          .header("Content-Disposition", "attachment; filename=" + "blah.pdf")
          .body(contents);

I get the error:

MessageBodyWriter not found for media type=application/pdf

and is there somewhere to find clear documentation of this stuff? very hard to google for.

I found that just returning raw "byte[]" from my resource method makes it work, or a raw Response holding byte[] content. still strange that I can't get ResponseEntity to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM