gpt4 book ai didi

spring-boot - Swagger OpenAPI v3 文件下载问题(SpringBoot)

转载 作者:行者123 更新时间:2023-12-05 06:17:36 27 4
gpt4 key购买 nike

如何使用 Swagger OpenAPI v3 实现下载文件?

我无法弄清楚如何通过我的 Controller 在响应中返回实际文件内容。它不使用 OpenAPI 从字面上很简单,但在这里我必须使用通过 OpenAPI 接口(interface)生成的覆盖方法。

当我到达终点时,文件正在下载,但其中没有任何内容。

Swagger yaml 文件:

 responses:
'200':
description: OK
content:
application/octet-stream:
schema:
type: string
format: base64

Spring Boot Controller :

 @Override
public ResponseEntity<String> getFile(String storageIdentifier) throws IOException {

return ResponseEntity.ok().contentType(contentType(extension.get().toString()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
.body(new String(bytearray));
}

当我看到 Swagger API 文档时,它建议使用类型:字符串和格式:二进制(返回文件)或类型:字符串和格式:base64(返回字符串)

最佳答案

您需要实现一种方法或服务来提供存储中的文件(例如,我使用 AWS 存储桶)。

这是我的 Controller 现在的样子:

    @Operation(summary = "Download an image.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200",
description = "Successfully downloaded the image",
content = @Content(mediaType = "application/json",
schema = @Schema(type = "String", format = "byte")))
})
@GetMapping(value = PREFIX + "{articleId}/{fileName}")
public byte[] download(@PathVariable String articleId, @PathVariable String fileName) {
return imageService.get(articleId, fileName);
}

Json 格式的规范文件 ( https://stackoverflow.com/a/61334534/3595831 )。应该看起来像这样:

responses:
"200":
description: Successfully downloaded the image
content:
application/json:
schema:
type: String
format: byte

关于spring-boot - Swagger OpenAPI v3 文件下载问题(SpringBoot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61571632/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com