我正在使用 swagger-codegen-maven-plugin 从 OpenAPI 文件(OpenAPI 3.0.2)生成 Spring 接口(interface)
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.14</version>
一个rest API的响应应该是PDF文件
components:
schemas:
contractFile:
type: string
format: binary
生成的 Java REST 接口(interface)则包含以下方法
default ResponseEntity<File> getContract(@ApiParam(value = "File to download",required=true) @PathVariable("uid") String uid) {
...
}
File 类表示文件系统的路径,但我在文件系统上没有文件,只有 java 内存中的字节,我不想将它们作为文件保存到磁盘。
我希望 getContract 从内存中的 Stream/bytes 返回一些 StreamResource 或文件的一些其他表示形式。是否可以通过 swagger-codegen-maven-plugin 或其他选项来实现?
最佳答案
最后,我从
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.14</version>
...
</plugin>
到
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.2.3</version>
...
</plugin>
这个插件做对了并生成 Resource 而不是 ResponseEntity
关于java - Swagger codegen to Java Spring 从二进制格式的 OpenAPI 组件生成不正确的文件响应实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60192594/