zac*_*ach 7 java reflection annotations hibernate-validator
是否可以访问字段值,其中字段名称在注释中描述,注释对类中的另一个字段进行注释。
例如:
@Entity
public class User {
@NotBlank
private String password;
@Match(field = "password")
private String passwordConfirmation;
}
Run Code Online (Sandbox Code Playgroud)
注解:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface Match {
String message() default "{}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String field();
}
Run Code Online (Sandbox Code Playgroud)
现在,是否可以从 ConstraintValidator 实现类中的 User 类访问字段密码?
编辑:
我写了这样的东西:
public class MatchValidator implements ConstraintValidator<Match, Object> {
private String mainField;
private String secondField;
private Class clazz;
@Override
public void initialize(final Match match) {
clazz = User.class;
final Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Match.class)) {
mainField = field.getName();
}
}
secondField = match.field();
}
@Override
public boolean isValid(final Object value, final ConstraintValidatorContext constraintValidatorContext) {
try {
Object o; //Now how to get the User entity instance?
final Object firstObj = BeanUtils.getProperty(o, mainField);
final Object secondObj = BeanUtils.getProperty(o, secondField);
return firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
} catch (final Exception ignore) {
ignore.printStackTrace();
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是如何获取 User 对象实例并比较字段值?
zac*_*ach 11
@Hardy Thenks 给点小费。最后写了一些符合(或多或少)预期结果的代码。
我会把它贴在这里,也许会帮助某人解决他的问题。
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Match {
String field();
String message() default "";
}
Run Code Online (Sandbox Code Playgroud)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = MatchValidator.class)
@Documented
public @interface EnableMatchConstraint {
String message() default "Fields must match!";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Run Code Online (Sandbox Code Playgroud)
public class MatchValidator implements ConstraintValidator<EnableMatchConstraint, Object> {
@Override
public void initialize(final EnableMatchConstraint constraint) {}
@Override
public boolean isValid(final Object o, final ConstraintValidatorContext context) {
boolean result = true;
try {
String mainField, secondField, message;
Object firstObj, secondObj;
final Class<?> clazz = o.getClass();
final Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Match.class)) {
mainField = field.getName();
secondField = field.getAnnotation(Match.class).field();
message = field.getAnnotation(Match.class).message();
if (message == null || "".equals(message))
message = "Fields " + mainField + " and " + secondField + " must match!";
firstObj = BeanUtils.getProperty(o, mainField);
secondObj = BeanUtils.getProperty(o, secondField);
result = firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
if (!result) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(message).addPropertyNode(mainField).addConstraintViolation();
break;
}
}
}
} catch (final Exception e) {
// ignore
//e.printStackTrace();
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
以及如何使用它...?像这样:
@Entity
@EnableMatchConstraint
public class User {
@NotBlank
private String password;
@Match(field = "password")
private String passwordConfirmation;
}
Run Code Online (Sandbox Code Playgroud)
您要么需要编写一个类级别约束,在其中将完整的User实例传递到isValid调用中,要么您可以使用@ScriptAssert之类的东西。
目前,无法将根 bean 实例作为“正常”字段验证的一部分进行访问。有一个 BVAL 问题 - BVAL-237 - 讨论添加此功能,但到目前为止它还不是 Bean 验证规范的一部分。
请注意,根 bean 无法访问 atm 是有充分理由的。对于validateValue情况,依赖于根 bean 可访问的约束将失败。