update 更新 枚举校验通过枚举字段的getter方法取值
Signed-off-by: 秋辞未寒 <545073804@qq.com>
This commit is contained in:
parent
a1f82a7d08
commit
4821902fdc
@ -9,7 +9,7 @@ import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* 自定义枚举校验注解
|
||||
* 自定义枚举校验
|
||||
*
|
||||
* @author 秋辞未寒
|
||||
* @date 2024-12-09
|
||||
@ -27,9 +27,10 @@ public @interface EnumPattern {
|
||||
Class<? extends Enum> type() default Enum.class;
|
||||
|
||||
/**
|
||||
* 枚举类型校验值判断方法
|
||||
* 枚举类型校验值字段名称
|
||||
* TODO 需确保该字段实现了 getter 方法
|
||||
*/
|
||||
String method() default "getCode";
|
||||
String fieldName() default "code";
|
||||
|
||||
String message() default "输入值不在枚举范围内";
|
||||
|
||||
|
@ -28,10 +28,10 @@ public class EnumPatternValidator implements ConstraintValidator<EnumPattern, St
|
||||
try {
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
Class<?> type = annotation.type();
|
||||
String fieldName = annotation.fieldName();
|
||||
Object[] enumConstants = type.getEnumConstants();
|
||||
Method method = ReflectUtils.getMethod(type, annotation.method());
|
||||
for (Object e : enumConstants) {
|
||||
if (value.equals(method.invoke(e))) {
|
||||
if (value.equals(ReflectUtils.invokeGetter(e, fieldName))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user