我正在尝试为我的 API 中的所有端点添加一个默认错误模型,以获取一些错误代码。
我通过阅读以下问题找到了部分解决方案:
这是我为该定制创建的 bean:
@Bean
public OpenApiCustomiser customOpenApiCustomiser() {
return openApi -> {
openApi.getPaths().values().forEach(pathItem -> pathItem.readOperations().forEach(operation -> {
Schema sharedErrorSchema = ModelConverters.getInstance()
.read(Error.class)
.getOrDefault("Error", new Schema());
MediaType sharedMediaType = new MediaType().schema(sharedErrorSchema);
Content sharedContent = new Content()
.addMediaType(APPLICATION_JSON_VALUE, sharedMediaType);
ApiResponses apiResponses = operation.getResponses();
ApiResponse response = new ApiResponse()
.description("Unhandled server error")
.content(sharedContent);
apiResponses.addApiResponse("500", response);
}));
};
}
我的错误类看起来像:
public class Error {
private String message;
private List<ErrorItem> errorItems;
}
问题是,当我在 swagger-ui 中打开端点定义之一时,我收到以下错误:
Could not resolve reference: Could not resolve pointer: /components/schemas/ErrorItem does not exist in document
如何定义 ErrorItem 的架构,使其可用于 swagger-ui?
我在用:
- Spring Boot => 2.2.4.Release
- springdoc-openapi-ui => 1.3.0
- springdoc-openapi-security => 1.3.0