class Entity {
@NotEmpty(message = “xxx”)
private int code;
}
public R save(@Valid @requestBody Entity entity,BindingResult result) {
}
@RestControllerAdvice=(@ResponseBody + @ControllerAdivce)
MethodArgumentNotValidException{
}
///分组校验——某字段新增和修改需求不同
空接口
public interface AddGroup {
}
Class Entity {
@Null(message=“x”,groups= {UpdateGroup.class})
@NotNull(message=“x”,groups={ })
Private int bid;
}
public R save(@Validated({AddGroup.class}) @requestBody Entity entity,BindingResult result) {
}
///自定义校验
一个校验注解可以配置多个校验器,自动识别
模仿@NotBlank写 @ListValue
ListValueConstraintValidator 实现Constraint Validator接口
@Contraint(validateBy = {ListValueConstraintValidator.class })
public @interface ListValue {
}