问题描述
我在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;
};
}