Double 类型的 Hibernate @Digits 验证
Hibernate @Digits Validation for Double type
我正在尝试借助 Hibernate 验证 API 验证 Double
字段 - 使用 @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 位> 预期)
您已将小数部分的位数限制为 0
,这意味着不允许使用小数,即使您的 0.0
中没有任何小数,但您应该确认情况确实如此,我认为你有一个 Double
值是 0 < x < 0.1
你也可以尝试对同一个字段使用 Float
,并检查是否有同样的问题?
The value of the field or property must be a number within a specified range. The integer element specifies the maximum integral digits for the number, and the fraction element specifies the maximum fractional digits for the number.
或者,您也可以试试;
@DecimalMin("0.00")
@DecimalMax("99999999999999999.00")
我认为 buildArea 的主要问题是它的数据类型 Double。
https://www.owasp.org/index.php/Bean_Validation_Cheat_Sheet
上面link说@Digits允许的数据类型不支持Double。
我不明白。如果小数部分只有 1 位,比如 0.0
,为什么要限制在 @Digits
和 fraction=0
?该限制是为了确保您没有小数,因此在您的情况下只有整数有效。
这是有效的:
@Digits(integer=10, fraction=1)
private Double double1 = 0.0;
这也是有效的:
@Digits(integer=10, fraction=0)
private Integer integer1 = 0;
但这是无效的:
@Digits(integer=10, fraction=0)
private Double double1 = 0.0; // Double not allowing a fraction part = integer, so you must change type here.
我正在尝试借助 Hibernate 验证 API 验证 Double
字段 - 使用 @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 位> 预期)
您已将小数部分的位数限制为 0
,这意味着不允许使用小数,即使您的 0.0
中没有任何小数,但您应该确认情况确实如此,我认为你有一个 Double
值是 0 < x < 0.1
你也可以尝试对同一个字段使用 Float
,并检查是否有同样的问题?
The value of the field or property must be a number within a specified range. The integer element specifies the maximum integral digits for the number, and the fraction element specifies the maximum fractional digits for the number.
或者,您也可以试试;
@DecimalMin("0.00")
@DecimalMax("99999999999999999.00")
我认为 buildArea 的主要问题是它的数据类型 Double。
https://www.owasp.org/index.php/Bean_Validation_Cheat_Sheet
上面link说@Digits允许的数据类型不支持Double。
我不明白。如果小数部分只有 1 位,比如 0.0
,为什么要限制在 @Digits
和 fraction=0
?该限制是为了确保您没有小数,因此在您的情况下只有整数有效。
这是有效的:
@Digits(integer=10, fraction=1)
private Double double1 = 0.0;
这也是有效的:
@Digits(integer=10, fraction=0)
private Integer integer1 = 0;
但这是无效的:
@Digits(integer=10, fraction=0)
private Double double1 = 0.0; // Double not allowing a fraction part = integer, so you must change type here.