-
@Length,@MaxLength,@MinLength
maxLength,minLength只有value属性,表示和value进行比较
min和max是Length的属性
汉字算一个长度
-
@NotBlank、@NotNull、@NotEmpty
NotBlank:Check if the string representation is not empty and does not only contain white spaces.
NotNull:Check if not null.
NotEmpty:Check if the string representation is not empty ("").
- @CheckWith
Check the value by a method of the sameclass that takes the value as argument and returns true if valid and false ifinvalid.
使用net.sf.oval.constraint.CheckWithCheck.SimpleCheck实现该接口的类中isSatisfied方法来判断,返回true有效,false无效;如果实现类是内部的,必须为静态类。
示例:验证User类中的age字段
@CheckWith(value=CheckAge.class,message="agemust in (18~65)")
private int age;
验证类如下:
publicclass CheckAge implements CheckWithCheck.SimpleCheck {
private static final long serialVersionUID =1L;
@Override
public boolean isSatisfied(Object validatedObject, Object value) {
if (!validatedObject instanceof User) {
return false;
}
User user = (User)validatedObject;
int age = user.getAge();
if(age <18 || age > 65)
return false;
else
return true;
}
}
- 完全验证
- 选择性验证(多个验证选择性验证)
List<ConstraintViolation> message = validator.validate(entity, profiles);
根据profiles进行选择性验证entity对象,可以指定多个profiles
- 验证对象的某个字段
public List<ConstraintViolation> validateFieldValue(final Object validatedObject, final Field validatedField,final Object fieldValueToValidate)