简体   繁体   English

如何在 OpenAPI 中自定义响应实体的主体类型?

[英]How to customise the body type of response entity in OpenAPI?

I am working on a Spring project that allows users to download CSV files from an endpoint.我正在开发一个 Spring 项目,该项目允许用户从端点下载 CSV 文件。

OpenAPI Generator is used for code generation. OpenAPI Generator 用于代码生成。 According to the "Response That Returns a File" section of the documentation , the schema for the response can be defined as type: string with format: binary .根据文档的“返回文件的响应”部分,响应的架构可以定义为type: stringformat: binary The generated interface would then be something like: CompletableFuture<ResponseEntity<org.springframework.core.io.Resource>> downloadFile() .生成的界面将类似于: CompletableFuture<ResponseEntity<org.springframework.core.io.Resource>> downloadFile()

How can we specify an alternative body type for ResponseEntity ?我们如何为ResponseEntity指定替代的主体类型? (eg StreamingResponseBody described in this blog ) (例如本博客中描述的StreamingResponseBody

The OpenAPI Generator has a configuration option named typeMappings which allows you to change the default mapping of OpenApi primitive types (such as array, float, etc...) to the corresponding java class (java.util.List, Float, ...). OpenAPI 生成器有一个名为typeMappings的配置选项,它允许您将 OpenApi 基本类型(例如数组、浮点数等)的默认映射更改为相应的 java class (java.util.List, Float, ... )。

In your particular case, you could try to map the "binary" type to something different from "java.util.File".在您的特定情况下,您可以尝试将“二进制”类型 map 转换为与“java.util.File”不同的东西。

The default mappings are defined here - see as from line 1464默认映射在此处定义 - 参见第 1464 行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从响应实体获取正文 - how to get body from the response entity 如何在 Spring Boot 中使用 OpenAPI 3 从“响应”和“请求正文”中隐藏“模式”? - How to hide "Schema" from "response" and "Request body" using OpenAPI 3 in Spring Boot? 如何将包裹在通用实体类型中的实体主体发送到 Spring API - How to send entity body wrapped in a generic entity type to an Spring API 如何在springboot中根据请求类型返回响应实体类型? - How to return response entity type according to request type in springboot? 如何自定义 javax.validation.Valid 错误响应 - How to customise javax.validation.Valid error response 在 Java 中验证 OpenAPI 响应 - Validate OpenAPI response in java Swagger codegen 到 Java Spring 从二进制格式的 OpenAPI 组件生成不正确的文件响应实体 - Swagger codegen to Java Spring generates incorrect file response entity from OpenAPI component of binary format 如何在 Springdoc OpenAPI 中隐藏特定端点的 ExceptionHandlers 响应 - How to hide the response from ExceptionHandlers for specific endpoints in Springdoc OpenAPI JSON 类型数组的响应体到 Java Object - JSON Response Body of type array to Java Object 如何在 OkHttp Response 中更改正文? - How to change body in OkHttp Response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM