快速进行数据验证的优雅实现-注解
javax.validation
包下的注解主要用于数据验证,确保数据符合特定的约束条件。以下是一个详细的表格,列出了这些注解的名称、作用、使用场景和示例:
Excel 表格示例
注解名称 | 作用 | 使用场景 | 示例 |
---|---|---|---|
@AssertFalse | 确保字段值为 false | 布尔字段的验证 | @AssertFalse private boolean active; |
@AssertTrue | 确保字段值为 true | 布尔字段的验证 | @AssertTrue private boolean active; |
@DecimalMax | 确保字段值小于或等于指定的最大值 | 数值字段的验证 | @DecimalMax("100.00") private BigDecimal price; |
@DecimalMin | 确保字段值大于或等于指定的最小值 | 数值字段的验证 | @DecimalMin("10.00") private BigDecimal price; |
@Digits | 确保字段值的数字部分和小数部分的位数符合指定要求 | 数值字段的验证 | @Digits(integer=5, fraction=2) private BigDecimal amount; |
@Future | 确保字段值是一个未来的日期 | 日期字段的验证 | @Future private Date startDate; |
@FutureOrPresent | 确保字段值是一个未来的日期或当前日期 | 日期字段的验证 | @FutureOrPresent private LocalDate startDate; |
@Max | 确保字段值小于或等于指定的最大值 | 数值字段的验证 | @Max(100) private int age; |
@Min | 确保字段值大于或等于指定的最小值 | 数值字段的验证 | @Min(18) private int age; |
@NotNull | 确保字段值不为 null | 通用字段的验证 | @NotNull private String name; |
@Null | 确保字段值为 null | 通用字段的验证 | @Null private String name; |
@Past | 确保字段值是一个过去的日期 | 日期字段的验证 | @Past private Date birthDate; |
@PastOrPresent | 确保字段值是一个过去的日期或当前日期 | 日期字段的验证 | @PastOrPresent private LocalDate birthDate; |
@Pattern | 确保字段值符合指定的正则表达式 | 字符串字段的验证 | @Pattern(regexp="^[a-zA-Z0-9_]+$") private String username; |
@Size | 确保字段值的长度在指定范围内 | 字符串、集合、数组等的验证 | @Size(min=5, max=10) private String password; |
@Range | 确保字段值在指定范围内 | 数值字段的验证 | @Range(min=18, max=60) private int age; |
@Valid | 用于嵌套验证,确保对象的属性也符合验证规则 | 对象字段的验证 | @Valid private User user; |
@Validate | 用于嵌套验证,确保对象的属性也符合验证规则 | 对象字段的验证 | @Validate private User user; |
@Validated | 用于类或方法级别的验证 | 类或方法的验证 | @Validated public class MyService {} |
@Constraint | 定义自定义验证注解 | 创建自定义注解时 | @Constraint(validatedBy = MyValidator.class) @interface MyCustomAnnotation {} |
@GroupSequence | 定义验证组的顺序 | 创建自定义验证组时 | @GroupSequence({Default.class, MyGroup.class}) public class MyEntity {} |
@GroupSequenceProvider | 提供动态验证组的顺序 | 创建自定义验证组时 | @GroupSequenceProvider(MyGroupSequenceProvider.class) public class MyEntity {} |
DEMO示例:
import javax.validation.constraints.*;
public class User {
@NotNull(message = "Name cannot be null")
@Size(min = 2, max = 50, message = "Name must be between 2 and 50 characters")
private String name;
@Min(value = 18, message = "Age must be at least 18")
@Max(value = 100, message = "Age must be at most 100")
private int age;
@NotNull(message = "Email cannot be null")
@Email(message = "Email must be valid")
private String email;
@NotNull(message = "Password cannot be null")
@Size(min = 8, max = 50, message = "Password must be between 8 and 50 characters")
private String password;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}