如何解决从类中获取具有Springdoc中泛型类型字段的架构
我在springboot中使用springdoc-openapi拥有rest服务,并且我的所有端点都使用相同的Class进行响应,如下所示:
public class ListResponse {
private List<?> list;
private Integer statusHttp;}
,然后在“列表”字段中添加一个bean的差异列表, 问题是,当我使用springdocs生成yaml定义时,它仅为所有端点生成以下模式:
ListResponse:
type: object
properties:
lista:
type: array
items:
type: object
statusHttp:
type: integer
format: int32
有人知道如何使用我的类ListResponse为端点生成自定义架构,还是应该为每个具有不同字段“ list”的端点生成一个类?
解决方法
您可以使用OperationCustimizer,在其中可以访问ApiResponse架构。
这是一个适合您键入的示例代码:
@Bean
public OperationCustomizer convertOperationsToString() {
(Operation operation,HandlerMethod handlerMethod) -> {
if (handlerMethod.getReturnType().getParameterType().isAssignableFrom(Duration.class)) {
for (Map.Entry<String,io.swagger.v3.oas.models.responses.ApiResponse> entry: operation.getResponses().entrySet()) {
io.swagger.v3.oas.models.responses.ApiResponse response = entry.getValue();
Content content = response.getContent();
if (content.containsKey(MediaType.APPLICATION_JSON_VALUE)) {
Schema schema = content.get(MediaType.APPLICATION_JSON_VALUE).getSchema();
schema.getProperties().clear();
schema.setType("string");
}
}
}
return operation;
};
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。