@Getter
@Setter
public class CountryTypeView {
private TargetType type;
private List countryList;
}
@Getter
@Setter
public class AuthSettingView {
@Size(min = 1, message = MessageKeys.USERS_SIZE)
private List users;
private String service;
@Size(min = 1, message = MessageKeys.CATEGORIES_SIZE)
private List categories;
@NotNull(message = MessageKeys.COUNTRIES_VALUE_SIZE)
@Valid
@CountrySize
private CountryTypeView countryType;
}
校验这个类,countryList 在type != “all” 的时候不能为空
自定义注解
@Constraint(validatedBy = CountrySizeValidator.class)
@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CountrySize {
String message() default MessageKeys.COUNTRIES_VALUE_SIZE;
int min() default 0;
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
自定义注解规则
public class CountrySizeValidator implements ConstraintValidator<CountrySize, CountryTypeView> {
@Override
public void initialize(CountrySize country) {
}
@Override
public boolean isValid(CountryTypeView value, ConstraintValidatorContext constraintValidatorContext) {
if (!TargetType.All.equals(value.getType()) && CollectionUtils.isEmpty(
value.getCountryList())) {
return false;
}
return true;
}
}
搞定!!!,如果帮到你,帮忙点个赞!!谢谢!!