Reputation: 2416
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.
Upvotes: 1
Views: 1316
Reputation: 1835
How about giving a responseCode
of 200
in ApiResponse
?
@GetMapping(value = "/pdf-file", produces = MediaType.APPLICATION_PDF_VALUE)
@ApiResponse(content = @Content(schema = @Schema(type = "string", format = "binary")), responseCode = "200")
public ResponseEntity<byte[]> previewPerformanceReport(...
A bit out of context but here's my exact problem FWIW.
Upvotes: 0
Reputation: 2416
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.
Upvotes: 0