是的,Swagger在Linux系统上是支持自定义注解的。您可以通过扩展Swagger的注解来实现自定义需求。以下是具体介绍:
@MyApiModel
和@MyApiModelProperty
,来扩展Swagger文档的内容。@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyApiModel {
String value() default "";
String description() default "";
Class<?>[] subTypes() default {};
String discriminator() default "";
Class<?>[] subTypes() default {};
String reference() default "";
}
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyApiModelProperty {
String value() default "";
String name() default "";
String allowableValues() default "";
String access() default "";
String notes() default "";
String dataType() default "";
boolean required() default false;
int position() default 0;
boolean hidden() default false;
String example() default "";
boolean readOnly() default false;
String reference() default "";
boolean allowEmptyValue() default false;
}
@Component
@Primary
public class MyModelBuilderPlugin implements ModelBuilderPlugin {
private final TypeResolver typeResolver;
@Autowired
public MyModelBuilderPlugin(TypeResolver typeResolver) {
this.typeResolver = typeResolver;
}
@Override
public void apply(ModelContext context) {
MyApiModel annotation = AnnotationUtils.findAnnotation(context.getType(), MyApiModel.class);
if (annotation != null) {
context.getBuilder().description(annotation.description());
}
}
private Class<?> forClass(ModelContext context) {
return typeResolver.resolve(context.getType()).getErasedType();
}
@Override
public boolean supports(DocumentationType delimiter) {
return true;
}
}
@Component
public class MyApiModelPropertyPropertyBuilder implements ModelPropertyBuilderPlugin {
private final DescriptionResolver descriptions;
@Autowired
public MyApiModelPropertyPropertyBuilder(DescriptionResolver descriptions) {
this.descriptions = descriptions;
}
@Override
public void apply(ModelPropertyContext context) {
Optional<MyApiModelProperty> annotation = context.getAnnotatedElement().map(element -> findApiModelPropertyAnnotation(element));
if (annotation.isPresent()) {
context.getBuilder().allowableValues(annotation.get().transform(toAllowableValues()).orNull())
.required(annotation.get().transform(toIsRequired()).or(false))
.readOnly(annotation.get().transform(toIsReadOnly()).or(false))
.description(annotation.get().transform(descriptions).orNull())
.isHidden(annotation.get().transform(toHidden()).or(false))
.type(annotation.get().transform(toType(context.getRes));
}
}
private Optional<MyApiModelProperty> findApiModelPropertyAnnotation(AnnotatedElement element) {
return Stream.of(element.getAnnotations())
.filter(annotation -> annotation.annotationType().equals(MyApiModelProperty.class))
.findFirst();
}
private Class<?> transformToType(ModelPropertyContext context) {
// 实现类型转换逻辑
return null;
}
}
确保在Swagger配置类中注册这些插件,以便Swagger在生成文档时使用自定义注解。
通过上述步骤,您可以在Linux系统上使用Swagger自定义注解来增强API文档的内容和结构。