【发布时间】:2018-07-10 19:07:15
【问题描述】:
我正在尝试寻找某种方法来记录返回 PDF(或任何其他文件)以供下载的 API。
使用Spring,我的Rest资源是这样的:
@Override
@GetMapping(produces = "application/pdf")
public ResponseEntity<InputStreamResource> find(
@PathVariable long id
) {
Result result = service.find(id);
HttpHeaders headers = disableCache();
return ResponseEntity
.ok()
.headers(headers)
.contentLength(result.getSize())
.contentType(MediaType.parseMediaType("application/pdf"))
.body(new InputStreamResource(result.getFileInputStream()));
}
这非常适合下载文件。但我不知道使用 Swagger 记录响应的良好做法。
实际上,我用 Swagger 注释尝试过:
@ApiOperation(value = "Some description")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success.")
})
@ResponseStatus(HttpStatus.OK)
@GetMapping(produces = "application/pdf")
ResponseEntity<InputStreamResource> find(
@PathVariable long id
);
但是 Swagger 在 Swagger-ui 上将 InputStreamResource 的内容作为 Json 返回,结果是什么不是。
如何在 Swagger 的响应中表示文件下载?
【问题讨论】: