找不到用于自定义注释的验证器和枚举Springboot Java8的验证
获取此错误HV000030:对于“ Myenum”
的约束验证类型的Myenum检查配置,找不到验证器。
使用此类的注释要验证,我不能做私有最终字符串myenum
,因为我需要将其用作枚举,以便在枚举
public class Request {
@EnumChecker(enumclass = MyEnum.class)
private final MyEnum myenum;
}
注释
@Documented
@Constraint(validatedBy = MyEnumValidator.class)
@Target({FIELD, PARAMETER, TYPE, TYPE_USE, TYPE_PARAMETER, METHOD, CONSTRUCTOR, ANNOTATION_TYPE})
@Retention(RUNTIME)
public @interface EnumChecker {
String message() default "must be one of these values";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
Class<? extends Enum> enumClass();
}
验证器
public class MyEnumValidator implements ConstraintValidator<EnumChecker, String> {
private List<String> validEnumList;
@Override
public void initialize(EnumChecker constraintAnnotation) {
validEnumList = Arrays.stream(constraintAnnotation.enumClass().getEnumConstants())
.map(Enum::name)
.collect(Collectors.toList());
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return validEnumList.contains(value);
}
}
枚举中使用该函数
public enum MyEnum {
YES, NO;
private static final Map<MyEnum, String> MyEnumConverter =
Map.of(YES, "yes");
public String toMyString() {
return MyEnumConverter.get(this);
}
}
Getting this error HV000030: No validator could be found for constraint EnumChecker validating type MyEnum Check configuration for ‘myenum’
.
Using the annotation on this class to be validated, I can't do private final String myenum
because I need to use it as an enum in order to use the function inside the enum
public class Request {
@EnumChecker(enumclass = MyEnum.class)
private final MyEnum myenum;
}
annotation
@Documented
@Constraint(validatedBy = MyEnumValidator.class)
@Target({FIELD, PARAMETER, TYPE, TYPE_USE, TYPE_PARAMETER, METHOD, CONSTRUCTOR, ANNOTATION_TYPE})
@Retention(RUNTIME)
public @interface EnumChecker {
String message() default "must be one of these values";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
Class<? extends Enum> enumClass();
}
validator
public class MyEnumValidator implements ConstraintValidator<EnumChecker, String> {
private List<String> validEnumList;
@Override
public void initialize(EnumChecker constraintAnnotation) {
validEnumList = Arrays.stream(constraintAnnotation.enumClass().getEnumConstants())
.map(Enum::name)
.collect(Collectors.toList());
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return validEnumList.contains(value);
}
}
enum
public enum MyEnum {
YES, NO;
private static final Map<MyEnum, String> MyEnumConverter =
Map.of(YES, "yes");
public String toMyString() {
return MyEnumConverter.get(this);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先删除
myenum
中的final
修饰符,并为该字段创建 setter 和 getter,因为Jackson
无法将 http 请求中的值绑定到您的 <例如 code>Request 类。然后更改
ConstraintValidator
目标类型以匹配MyEnum
而不是String
测试:我做了以下测试
我还更改了实现
MyEnumValidator
仅查看验证效果并证明其发生。 (我承认目前这个验证器没有意义。同时使用 only@RequestBody
保证myenum
只能有YES 或
NO
值,否则会引发异常)对于无效案例:
堆栈跟踪:
First delete
final
modifier inmyenum
and create setter and getter for that field, becauseJackson
is not able to bind values from http request to yourRequest
class for example.Then change
ConstraintValidator
target type to matchMyEnum
instead ofString
Test: I did the following test
Also I changed the implementation of
MyEnumValidator
to only see validation effect and prove that it takes place. (I admit that currently this validator does not make sense. Also using only@RequestBody
guarantees thatmyenum
can only HaveYES
orNO
values otherwise throws exception)For a invalid case:
stacktrace: