Springboot @Validated注解详细说明
在Spring Boot中,@Validated注解用于验证请求参数。它可以应用在Controller类或方法上
1、引入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId>
</dependency>
2、参数说明与使用示例
注解 | 验证的数据类型 | 描述 |
---|---|---|
@NotNull | 任意类型 | 验证属性不能为null |
@NotBlank | 字符串 | 验证字符串属性不能为空且长度必须大于0 |
@Size(min,max ) | CharSequence Collection Map Array | 字符串:字符串长度必须在指定的范围内 Collection:集合大小必须在指定的范围内 Map:map的大小必须在指定的范围内 Array:数组长度必须在指定的范围内 |
@Min | 整型类型 | 验证数字属性的最小值 |
@Max | 整型类型 | 验证数字属性的最大值 |
@DecimalMin | 数字类型 | 验证数字属性的最小值(包括小数) |
@DecimalMax | 数字类型 | 验证数字属性的最大值(包括小数) |
@Digits(integer,fraction) | 数字类型 | 验证数字属性的整数位数和小数位数 |
字符串类型 | 验证字符串属性是否符合Email格式 | |
@Pattern | 字符串 | 验证字符串属性是否符合指定的正则表达式 |
@Positive | 数字类型 | 验证数值为正数 |
@PositiveOrZero | 数字类型 | 验证数值为正数或0 |
@Negative | 数字类型 | 验证数值为负数 |
@NegativeOrZero | 数字类型 | 验证数值为负数或0 |
@AssertTrue | 布尔类型 | 参数值必须为 true |
@AssertFalse | 布尔类型 | 参数值必须为 false |
@Past | 时间类型(Date) | 参数值为时间,且必须小于 当前时间 |
@PastOrPresent | 时间类型(Date) | 参数值为时间,且必须小于或等于 当前时间 |
@Future | 时间类型(Date) | 参数值为时间,且必须大于 当前时间 |
@FutureOrPresent | 时间类型(Date) | 参数值为时间,且必须大于或等于 当前日期 |
@Data
public class User {// @NotNull:验证属性不能为null。@NotNull(message = "用户名不能为null")private String username;// @NotBlank:验证字符串属性不能为空且长度必须大于0。@NotBlank(message = "用户名不能为空且长度必须大于0")private String username1;// @Size:验证字符串属性的长度范围。@Size(min = 6, max = 20,message = "密码最小6位,最长20位")private String password;// @Min:验证数字属性的最小值。@Min(value = 18,message = "年龄最小18岁")private int age;// @Max:验证数字属性的最大值。@Max(100)private int age1;// @DecimalMin:验证数字属性的最小值(包括小数)。@DecimalMin("0.01")private BigDecimal price;// @DecimalMax:验证数字属性的最大值(包括小数)。@DecimalMax("1000.00")private BigDecimal price1;// @Digits:验证数字属性的整数位数和小数位数。@Digits(integer = 3, fraction = 2)private BigDecimal weight;// @Email:验证字符串属性是否符合Email格式。@Emailprivate String email;// @Pattern:验证字符串属性是否符合指定的正则表达式。@Pattern(regexp = "[A-Za-z0-9]+")private String username2;// @Positive:验证数值为正数@Positiveprivate int age2;// @PositiveOrZero:验证数值为正数或0@PositiveOrZeroprivate int age3;// @Negative:验证数值为负数@Negativeprivate int age4;// @NegativeOrZero:验证数值为负数或0@NegativeOrZeroprivate int age5;// @AssertTrue:参数值必须为 true@AssertTrueprivate boolean hasMoney;// @AssertFalse:参数值必须为 false@AssertFalseprivate boolean hasMoney1;// @Past:参数值为时间,且必须小于 当前时间@Pastprivate Date time;// @PastOrPresent:参数值为时间,且必须小于或等于 当前时间@PastOrPresentprivate Date time1;// @Future:参数值为时间,且必须大于 当前时间@Futureprivate Date time2;// @FutureOrPresent:参数值为时间,且必须大于或等于 当前日期@FutureOrPresentprivate Date time3;
}
嵌套校验:
在被检验的字段上添加 @Valid 注解就可以实现嵌套检验
@Data
public class User3 {@ValidList<User2> user2List;@Validprivate User2 user2;
}@Dataclass User2 {@NotBlank(message = "用户名不能为空!")private String username;
}
3、校验注解的三个参数:
message
:自定义错误消息。可以通过该参数指定验证失败时返回的错误消息。
示例:
public class User {@NotBlank(message = "用户名不能为空")private String username;// getter and setter
}
groups
:分组校验。可以通过该参数指定在特定的验证分组中才进行验证。
示例:
public interface Group1 {}public interface Group2 {}public class User {@NotBlank(groups = Group1.class)private String username;@NotBlank(groups = Group2.class)private String password;// getter and setter
}
username属性只在Group1分组中进行验证,password属性只在Group2分组中进行验证。
payload
:用于携带额外的验证信息。可以通过该参数传递一些自定义的验证信息。(不常用)
示例:
public class User {@NotBlank(payload = {ValidationInfo.class})private String username;// getter and setter
}public class ValidationInfo {private String info;// getter and setter
}
当验证失败时,可以通过ConstraintViolation对象获取到ValidationInfo对象,并获取其中的验证信息。
Set<ConstraintViolation<User>> violations = validator.validate(user);
for (ConstraintViolation<User> violation : violations) {if (violation.getConstraintDescriptor().getPayload().contains(ValidationInfo.class)) {ValidationInfo validationInfo = violation.getConstraintDescriptor().getPayload(ValidationInfo.class);String info = validationInfo.getInfo();// 处理验证信息}
}
4、在post 和 get 请求上使用
在post上使用@Validated,get上直接放校验注解