在springboot后端开发中常需要对前端传参进行校验,如果全部放在controller或者service中,会让代码看起来非常的冗余,这个时候直接在传参上加上约束注解就很方便实用,简单总结了一下常用的注解及其实用的场景。
1、@NotNull:Integer、Bigdecimal、LocalDateTime、LocalDate、实体类
2、@NotBlank:String
3、@NotEmpty:List
4、@Size:字符串长度限制
5、@Future:未来时间,LocalDateTime、LocalDate
6、@Past:过去时间,LocalDateTime、LocalDate
7、@DecimalMin:Bigdecimal最小值
8、@DecimalMax:Bigdecimal最大值
9、@Max:Integer最大值
10、@Min:Integer最小值
11、@Pattern:正则表达式校验
例如:
@NotBlank(message = "新密码不能为空")