我正在尝试验证 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 位>)
最佳答案
或者,你也可以试试;
@DecimalMin("0.00")
@DecimalMax("99999999999999999.00")
关于java - Double 类型的 Hibernate @Digits 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42787028/