依赖注入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
使用方法
在controller层对象参数前加上@Validated,然后对象中对应字段使用对应的判断注解
如果字段为对象,该对象内的参数也想校验,则在对象上使用@Valid
分组校验
如果有参数只在部分接口需要校验,则在判断注解中添加group参数,指定判断分组(分组为新类,继承Default)
@NotNull(groups = {UpdateGroup.class} , message = "ID不能为空!")
private Integer menuId;
public CommonResponse update(@Validated(UpdateGroup .class) MenuReq menuReq){}
注解
@NotBlank
判空
@NotNull
判null
@Range
判范围
判邮箱格式
@Pattern
判正则