Bean 验证范围约束
Bean Validation range constraint
我需要对实体数据字段实施范围限制:
@Entity
@Table(name = "T_PAYMENT")
public class Payment extends AbstractEntity {
//....
//Something like that
@Range(minValue = 80, maxValue = 85)
private Long paymentType;
}
我已经创建了验证服务,但必须实施其中的许多案例。
我需要应用程序在插入的数字超出范围时抛出异常。
使用 hibernate-validator
依赖项你可以定义范围检查
@Min(value = 80)
@Max(value = 85)
private Long paymentType;
在pom.xml
下面添加依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>{hibernate.version}</version>
</dependency>
您需要 Hibernate 验证器 (see documentation)
Hibernate Validator
The Bean Validation reference implementation.
Application layer agnostic validation Hibernate Validator allows to
express and validate application constraints. The default metadata
source are annotations, with the ability to override and extend
through the use of XML. It is not tied to a specific application tier
or programming model and is available for both server and client
application programming. But a simple example says more than 1000
words:
public class Car {
@NotNull
private String manufacturer;
@NotNull
@Size(min = 2, max = 14)
private String licensePlate;
@Min(2)
private int seatCount;
// ...
}
对于整数和长整数你可以使用@Min(value = 80)
@Max(value = 85)
对于 BigDecimal @DecimalMin(value = "80.99999")
@DecimalMax(value = "86.9999")
我相信这就是您正在寻找的特定注释:
https://docs.jboss.org/hibernate/validator/4.1/api/org/hibernate/validator/constraints/Range.html
示例:
@Range(min = 1, max =12)
private String expiryMonth;
这也是一个更有用的注解,因为它可以处理字符串或数字变体,而无需像 @Max/@Min 那样使用两个注解。 @Size 与整数不兼容。
我需要对实体数据字段实施范围限制:
@Entity
@Table(name = "T_PAYMENT")
public class Payment extends AbstractEntity {
//....
//Something like that
@Range(minValue = 80, maxValue = 85)
private Long paymentType;
}
我已经创建了验证服务,但必须实施其中的许多案例。
我需要应用程序在插入的数字超出范围时抛出异常。
使用 hibernate-validator
依赖项你可以定义范围检查
@Min(value = 80)
@Max(value = 85)
private Long paymentType;
在pom.xml
下面添加依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>{hibernate.version}</version>
</dependency>
您需要 Hibernate 验证器 (see documentation)
Hibernate Validator
The Bean Validation reference implementation.
Application layer agnostic validation Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming. But a simple example says more than 1000 words:
public class Car { @NotNull private String manufacturer; @NotNull @Size(min = 2, max = 14) private String licensePlate; @Min(2) private int seatCount; // ... }
对于整数和长整数你可以使用@Min(value = 80)
@Max(value = 85)
对于 BigDecimal @DecimalMin(value = "80.99999")
@DecimalMax(value = "86.9999")
我相信这就是您正在寻找的特定注释: https://docs.jboss.org/hibernate/validator/4.1/api/org/hibernate/validator/constraints/Range.html
示例:
@Range(min = 1, max =12)
private String expiryMonth;
这也是一个更有用的注解,因为它可以处理字符串或数字变体,而无需像 @Max/@Min 那样使用两个注解。 @Size 与整数不兼容。