spring-boot - 如何根据 Swagger 的自定义 Annotation 创建注释/描述
问题描述
我有一个 Springboot Rest 应用程序,其中有自动转换 API 和参数的注释。
我有自定义注释,其中包含一些注释,如何在 OpenAPI 3 中将其生成到我的招摇页面?
Ex:
@RestController
Class Controller {
@GetMapping(/test/result/)
@CustomAnnotation(value = "This description should come in swagger")
void method() {
}
}
解决方案
SpringDoc 允许您通过实现自己的 Customizer bean 来自定义生成的 OpenAPI 规范。
有很多定制器界面可用于定制,但最有用的是OperationCustomizer
、ParameterCustomizer
和PropertyCustomizer
.
以下是您的用例的操作定制器示例。
@Component
public class OperationCustomizer implements org.springdoc.core.customizers.OperationCustomizer {
@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
CustomAnnotation annotation = handlerMethod.getMethodAnnotation(CustomAnnotation.class);
if (annotation != null) {
operation.description(annotation.value());
}
return operation;
}
}
在这里,您可以找到使用自定义注释和定制器的项目示例。
这里是一个基于 @NonNull 注释修改生成规范的项目示例。
推荐阅读
- c# - 文本更改事件未进入侦听器
- java - 如何使用 Angular 和 Spring Boot RESTful Api 清理或沙盒用户输入
- python - Kivy Android:无法在 TextInput 中输入日语(或中文)
- javascript - 组合大写和小写搜索选项不适用于角度
- python - twinx 弄乱了 pcolormesh 情节的颜色条
- asp.net-mvc - 动态使用openxml在ppt中的多张幻灯片中划分表格
- database - 无法在多节点 TimescaleDB 设置上创建分布式超表
- r - 有没有办法在 R 中使用 spatstat 创建 ppp 对象列表?
- python - 如何计算 Pytorch 中的平均相对绝对误差
- python - 如何使用 C# 调用 Python 3.9 脚本