依赖添加
org.springframework.bootgroupId> spring-boot-starter-validationartifactId>
dependency>
一些较老版本的SpringBoot需要添加相关依赖,我使用的2.1.4发行版不用这个操作。
验证使用对象接收参数的情况
public class PointDeductSetRequest{
private Long id;
@NotBlank(message = "租户id为空")
private String tenantId;
private Integer status;
@NotNull
private Integer pointValue;
@NotNull
private Integer deductValue;
@NotBlank(message = "操作员id为空")
private String operator;
}
首先在需要验证的对象的对应字段上方加上校验注解,以下为一些常用注解:
@Null 限制只能为null
@NotNull 限制必须不为null
@AssertFalse 限制必须为false
@AssertTrue 限制必须为true
@DecimalMax(value) 限制必须为一个不大于指定值的数字
@DecimalMin(value) 限制必须为一个不小于指定值的数字
@Digits(integer,fraction) 限制必须为一个小数&#