- 1、简介
- 2、Validator常见注解清单
- 3、校验案例实战
- 3.1、校验实体
- 3.2、校验Cntroller层实现
- 4、统一捕获验证注解异常
- 5、自定义验证异常
Spring Boot支持JSR-303验证框架,默认实现是Hibernate Validator,项目中只要在Java Bean上放一些校验注解,就可以实现校验支持,杜绝通篇 if else 参数判断,而且这个校验是支持group的概念的,对于不同的group生效的校验不一样。这个很有用,因为对于增删改查等不同的操作,需要执行的校验本来就是不一样的。
注意:
spring boot2.3.x版本的spring boot项目,在使用hibernate-validator相关的注解的时候,发现不能导入相关的包。
官方给出的解决方法,手动导入相关依赖就行,如下:
2、Validator常见注解清单org.springframework.boot spring-boot-starter-validation
| 注释 | 描述 |
|---|---|
| @AssertFalse | 被注释的参数必须为 false |
| @AssertTrue | 被注释的元素必须为 true |
| @NotNull | 参数不能是Null |
| @Null | 参数是Null |
| @Szie | 带注释的参数大小必须介于指定边界(包括)之间 |
| @PositiveOrZero | 被注释的元素必须严格的正数(包含0) |
| @Positive | 被注释的元素必须严格的正数(0为无效值) |
| @Pattern | 被注释的元素必须符合指定的正则表达式 |
| @NotEmpty | 同StringUtils.isNotEmpty |
| @NotBlank | 同StringUtils.isNotBlank |
| @NegativeOrZero | 带注释的元素必须是一个严格的负数(包含0) |
| @Negative | 带注释的元素必须是一个严格的负数(0为无效值) |
| @Min | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 |
| @Max | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 |
| 邮箱规则 | |
| @DecimalMax | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 |
| @DecimalMin | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 |
| @Range(min=1, max=99) | 参数需要在指定的范围内 |
@Data
//@TableName("t_teacher"),实体类名以驼峰的形式命名,可以省略@TableName,否则加上
public class TTeacher implements Serializable {
private static final long serialVersionUID = 1L;
public interface Update{}
public interface Add{}
@TableId(type = IdType.AUTO)
@NotNull(groups = {Update.class},message = "不能为空")
@Null(groups = {Add.class},message = "必须为空")
private Integer id;
//教师名称
@Size(min=3, max=20)
private String teacherName;
//教师年龄
private integer age;
//学科
private String course;
//评分
private BigDecimal score;
//教学天数
private Integer teachingDays;
//状态
private String status;
//授课总数
private Integer teachingCount;
//每天教学价格
private BigDecimal priceOfDay;
//开始教学时间
private Date startTime;
//简介
private String description;
//照片
private String photo;
//详细信息
private String detail;
}
3.2、校验Cntroller层实现
//如果想在参数中使用 @NotNull 这种注解校验,就必须在类上添加 @Validated
//如果方法中的参数是对象类型,则必须要在参数对象前面添加 @Validated
@Validated
@RestController
@RequestMapping("/teacher")
public class TeacherController {
@Autowired
private TeacherService teacherService;
@IgnoreAnnotation
@GetMapping(value = "/findAll",name = "查询所有老师数据")
public ResponseMessage> findAll(){
return new OverAllExceptionAdvice().sendSuccessResponse(teacherService.findAll());
}
// 这里就声明了要激活Add group对应的校验注解
// 那么就会校验tTeacher的id不能为空
@PostMapping(value = "/add",name = "新增教师")
public ResponseMessage add(@RequestBody @Validated({TTeacher.Add.class}) TTeacher tTeacher){
teacherService.add(tTeacher);
return new OverAllExceptionAdvice().sendSuccessResponse();
}
@PutMapping(value = "/update",name = "更新教师信息")
public ResponseMessage update(@RequestBody TTeacher tTeacher){
teacherService.update(tTeacher);
return new OverAllExceptionAdvice().sendSuccessResponse();
}
@DeleteMapping(value = "/del/{id}",name = "根据id删除教师信息")
public ResponseMessage del(@PathVariable("id") @NotNull @Min(value = 10,message = "id不能小于此数字") Integer id){
teacherService.del(id);
return new OverAllExceptionAdvice().sendSuccessResponse();
}
}
执行结果,http://localhost:8090/elasticsearch/teacher/add
http://localhost:8090/elasticsearch/teacher/del/5
@RestControllerAdvice @Slf4j public class OverAllExceptionAdviceimplements ResponseBodyAdvice
IgnoreAnnotation注解代码实现:
@documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreAnnotation {
}
5、自定义验证异常
代码实现:
@Constraint(validatedBy = {Agevalidator.class})
@documented
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicty.RUNTIME)
public @interface Age {
String message() default "年龄是非法的,不能超过{max}岁";
int max() default 100;
Class>[] groups default {};
Class extends Payload>[] payload() default {};
}
public class Agevalidator implements ConstraintValidator{ private Age age; private Integer max; public void initialize(Age age) { this.age = age; this.max = age.max(); } public boolean isValid(Integer value, ConstraintValidatorContext context) { return value < max; } }



