配置SpringDoc文档枚举值以及描述
创建自定义枚举 IEnum
import java.io.Serializable
interface IEnum<V : Serializable> {
@get:JsonValue
val value: V
companion object {
inline fun <reified B, V> Class<B>.fromValue(value: V): B?
where B : IEnum<V>, V : Serializable {
return enumConstants?.asSequence()
?.filter { it.value == value }
?.firstOrNull()
}
}
}
创建工具类
import io.swagger.v3.core.util.PrimitiveType
import io.swagger.v3.oas.models.media.ObjectSchema
import io.swagger.v3.oas.models.media.Schema
import org.springframework.beans.BeanUtils
import org.springframework.util.ReflectionUtils
import java.lang.reflect.Modifier
import java.lang.reflect.Type
interface IEnumCustomizer {
fun getValues(enumClazz: Class<*>): List<Any> {
return enumClazz.enumConstants
.mapNotNull { item ->
val getValue = ReflectionUtils.findMethod(item.javaClass, "getValue")
getValue?.let {
ReflectionUtils.makeAccessible(it)
ReflectionUtils.invokeMethod(it, item)
}
}
}
fun getDescription(enumClazz: Class<*>): String {
val fieldList = enumClazz.declaredFields
.filter { !Modifier.isStatic(it.modifiers) }
.sortedByDescending { it.name }
fieldList.forEach { ReflectionUtils.makeAccessible(it) }
return enumClazz.enumConstants
.filterNotNull().joinToString("; ") { item ->
fieldList.joinToString(" : ") { field ->
field.get(item).toString()
}
}
}
fun getSchemaByType(type: Type, sourceSchema: Schema<*>): Schema<Any> {
val schema = PrimitiveType.fromType(type)?.createProperty()
?: ObjectSchema()
BeanUtils.copyProperties(sourceSchema, schema)
return schema
}
}
创建SpringDoc参数自定义配置
import io.swagger.v3.oas.models.media.Schema
import io.swagger.v3.oas.models.parameters.Parameter
import org.springdoc.core.customizers.ParameterCustomizer
import org.springframework.core.MethodParameter
import org.springframework.stereotype.Component
@Component
class ApiEnumParameterCustomizer : ParameterCustomizer, IEnumCustomizer {
override fun customize(parameterModel: Parameter?, methodParameter: MethodParameter): Parameter? {
val parameterType = methodParameter.parameterType
parameterModel?.let {
if (IEnum::class.java.isAssignableFrom(parameterType)) {
it.description = getDescription(parameterType)
val schema = Schema<Any>()
schema.enum = getValues(parameterType)
it.schema = schema
}
}
return parameterModel
}
}
创建SpringDoc属性自定义配置
import com.fasterxml.jackson.databind.type.SimpleType
import io.swagger.v3.core.converter.AnnotatedType
import io.swagger.v3.oas.models.media.Schema
import org.springdoc.core.customizers.PropertyCustomizer
import org.springframework.stereotype.Component
import org.springframework.util.ObjectUtils
import java.lang.reflect.ParameterizedType
@Component
class ApiEnumPropertyCustomizer : PropertyCustomizer, IEnumCustomizer {
override fun customize(property: Schema<*>, type: AnnotatedType): Schema<*> {
if (type.type is SimpleType) {
val fieldClazz = (type.type as SimpleType).rawClass
if (IEnum::class.java.isAssignableFrom(fieldClazz)) {
val genericInterfaces = fieldClazz.genericInterfaces
if (genericInterfaces.isNotEmpty() && genericInterfaces[0] is ParameterizedType) {
val actualTypeArgument = (genericInterfaces[0] as ParameterizedType).actualTypeArguments[0]
val schema = getSchemaByType(actualTypeArgument, property)
schema.enum = this.getValues(fieldClazz)
val description = this.getDescription(fieldClazz)
schema.description = if (ObjectUtils.isEmpty(property.description)) description else "${property.description} ($description)"
return schema
}
}
}
return property
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/922b190233ca77c1c0be4dd7000c7578.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b8dfe1f1cb5bf4b618b5867212af555b.png)