开发中,通常操作逻辑都是先进性数据校验,校验完毕以后才进行真正的逻辑处理。通常情况下,可以使用javax.validation校验注解,类似于AOP面向切面的实现数据的校验。在springboot中引入spring-boot-starter-web依赖,就会自动引入hibernate-validator。如图:
![2b61d224d39b378492f2edb222ea8739.png](https://i-blog.csdnimg.cn/blog_migrate/752a4ad61f3da9209b3b56f1be6e1ed1.jpeg)
内置注解
使用javax.validation校验参数,一般在controller层,对方法参数进行校验。常用的注解包括:@NotNull,值不能为空,@Positive,数字为正数,@Size,字符串大小限制。示例:
/**
* @Author iloveoverfly
**/
@Data
public class UserAddDto implements Serializable {
private static final long serialVersionUID = -6630904002198113779L;
@NotEmpty(message = "用户名称为空")
private String username;
@NotNull(message = "用户的类型为空")
private Integer category;
}
</