javax.validation分组校验(简短版)
javax.validation注解校验:我们使用的实体,有时候需要全部校验它的字段,有时候其他地方使用只需要校验它的部分字段。
解决办法:使用分组校验。
public class Group { public interface AddGroup{} public interface UpdateGroup{} }
/** * 用户名 */ @NotBlank(message = "用户名不能为空!",groups = Group.AddGroup.class) private String userName; /** * 密码 */ @NotBlank(message = "密码不能为空!") private String password;
1.新增的时候我需要全部校验字段(userName,password都校验):
@RequestBody @Validated({Default.class, Group.AddGroup.class}) UserEntity userEntity, BindingResult bindingResult
或者
private BaseResponse validateParam(UserEntity userEntity) { List<String> validateError = ValidationUtil.validate(userEntity, Default.class, Group.AddGroup.class);//Default.class, Group.AddGroup.class if (validateError != null && validateError.size() > 0) { // validateError.get(0) return BaseResponse.failedCustom(validateError.get(0)).build(); } return BaseResponse.successCustom().build(); }
ValidationUtil:
import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import javax.xml.bind.ValidationException; import java.util.ArrayList; import java.util.List; import java.util.Set; /** * @description: 校验工具类 * @author liuyachao * @date 2021/9/18 16:38 */ public class ValidationUtil { private static Validator validator; static { ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); validator = vf.getValidator(); } /** * javax.validation注解校验 * @throws ValidationException * @throws ValidationException * @Description: 校验方法 * @param t 将要校验的对象 * @throws ValidationException * void * @throws */ public static <T> List<String> validate(T t){ return validate(t,null); } /** * javax.validation注解校验,分组校验 * @param t 校验实体 * @param groups 校验分组 * @return java.util.List<java.lang.String> */ public static <T> List<String> validate(T t, Class<?>... groups){ Set<ConstraintViolation<T>> set; if(groups != null && groups.length > 0){ set = validator.validate(t,groups); }else{ set = validator.validate(t); } List<String> validateError = new ArrayList<>(); if(set.size()>0){ for(ConstraintViolation<T> val : set){ validateError.add(val.getMessage()); } } return validateError; } }
2.其他地方使用我只需要校验部分字段(只校验password字段,属于默认分组Default):
@RequestBody @Validated({Default.class}) UserEntity userEntity, BindingResult bindingResult
或者
private BaseResponse validateParam(UserEntity userEntity) { List<String> validateError = ValidationUtil.validate(userEntity, Default.class);//Default.class, Group.AddGroup.class if (validateError != null && validateError.size() > 0) { // validateError.get(0) return BaseResponse.failedCustom(validateError.get(0)).build(); } return BaseResponse.successCustom().build(); }
3.只校验userName字段,属于分组AddGroup:
@RequestBody @Validated({Group.AddGroup.class}) UserEntity userEntity, BindingResult bindingResult
或者
private BaseResponse validateParam(UserEntity userEntity) { List<String> validateError = ValidationUtil.validate(userEntity,Group.AddGroup.class);//Default.class, Group.AddGroup.class if (validateError != null && validateError.size() > 0) { // validateError.get(0) return BaseResponse.failedCustom(validateError.get(0)).build(); } return BaseResponse.successCustom().build(); }
userName属于AddGroup分组,password属于Default默认分组(没有添加自定义分组的字段都默认为Default分组)。它们属于不同分组。校验的时候选择不同分组,只会校验选择的分组的字段。
详解版参考:https://www.cnblogs.com/super-chao/p/15314566.html
ValidationUtil