我正在尝试在Double
Hibernate 验证 API 的帮助下验证一个字段 - 使用@Digits
注释
<areaLength>0</areaLength>
<buildArea>0.0</buildArea>
@Digits(integer=10, fraction=0)
private Long areaLength = null; // Here areaLength = 0
@Digits(integer=20, fraction=0)
private Double buildArea = null; // Here buildArea = 0.0
这里areaLength
没有违反约束,
但buildArea
正在违反约束,说
buildArea numeric value out of bounds (<20 digits>.<0 digits> expected)
10.0 没有违规,但 0.0 违规。
有人知道原因吗?
完整代码:
public class ValidationTest {
public static void main(String a[]) {
Vehicle vehBean = new Vehicle();
try {
if (vehBean != null) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Vehicle>> violations = validator.validate(vehBean);
if (violations!= null && violations.size() > 0) {
for (ConstraintViolation<Vehicle> violation : violations) {
System.out.println("Validation problem : " + violation.getMessage());
}
}
}
} catch(Exception e) {
throw e;
}
}
}
class Vehicle {
@Digits(integer=20, fraction=0)
private Double buildArea = 0.0;
}
验证问题:数值超出范围(<20 位>.<0 位> 预期)