swagger-ui枚举类说明注解扩展

72 阅读1分钟

/**

  • 枚举类对象

  • @return

*/

Class<? extends BaseEnum> value();

String example() default "";

boolean required() default true;

String dataType() default "";

String enumDesc() default "";

}

2.创建需要引用的基础BaseEnum 枚举类

import java.util.Objects;

/**

  • @Description: 基础枚举

  • @Author: tarzan Liu

  • @Company: 华夏天信研究院

  • @Date: 2020/10/28 9:27

*/

public interface BaseEnum {

/**

  • 获取枚举类的值

  • @return Integer

*/

Object getValue();

/**

  • 获取枚举类的说明

  • @return String

*/

String getDesc();

/**

  • 比较参数是否与枚举类的value相同

  • @param value

  • @return boolean

*/

default boolean equalsValue(Object value) {

return Objects.equals(getValue(), value);

}

/**

  • 比较枚举类是否相同

  • @param baseEnum

  • @return boolean

*/

default boolean equals(BaseEnum baseEnum) {

return Objects.equals(getValue(), baseEnum.getValue()) && Objects.equals(getDesc(), baseEnum.getDesc());

}

/**

  • @Description:返回枚举类的说明

  • @Date: 2020/11/7 16:54

*/

static String getInfo(Class<? extends BaseEnum> clazz) {

BaseEnum[] enums = clazz.getEnumConstants();

String enumStr = " [ ";

for (BaseEnum e : enums) {

enumStr = enumStr + e.getValue() + " - " + e.getDesc()+";";

}

enumStr = enumStr + "]";

return enumStr;

}

}

3.创建所需要的枚举类继承基础BaseEnum 枚举类

import com.hxtx.stripmine.enums.BaseEnum;

/**

  • @Description:文件服务枚举类

  • @Author: tarzan LiU

  • @Company: 华夏天信研究院

  • @Date: 2020/11/25 11:13

*/

public enum FileServiceTypeEnum implements BaseEnum {

/**

  • 本地文件服务

*/

LOCAL(1, FileServiceNameConst.LOCAL, "本地文件服务"),

/**

  • 阿里OSS文件服务

*/

ALI_OSS(2, FileServiceNameConst.ALI_OSS, "阿里OSS文件服务"),

/**

  • 七牛文件服务

*/

QI_NIU_OSS(3, FileServiceNameConst.QI_NIU_OSS, "七牛文件服务"),

/**

  • minio文件服务

*/

MINIO_OSS(4, FileServiceNameConst.MINIO_OSS, "minio文件服务");

private Integer locationType;

private String serviceName;

private String desc;

FileServiceTypeEnum(Integer locationType, String serviceName, String desc) {

this.locationType = locationType;

this.serviceName = serviceName;

this.desc = desc;

}

public Integer getLocationType() {

return locationType;

}

public String getServiceName() {

return serviceName;

}

@Override

public Integer getValue() {

return this.locationType;

}

@Override

public String getDesc() {

return this.desc;

}

}

4.对象上使用自定义注解

import com.hxtx.stripmine.common.anno.ApiModelPropertyEnum;

import com.hxtx.stripmine.enums.file.FileServiceTypeEnum;

import io.swagger.annotations.ApiModelProperty;

import lombok.Data;