示例如下:和@Empty功能一样
/**
* Desc: 自定义list非空严重,验证list是否是null或空
*
* @author wangcen
* @date 2021/7/2 14:25
*/
@Documented
@Constraint(validatedBy = {ListCustomValid.class })
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ListNotEmpty {
String message() default "参数不能为空";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
/**
* Desc: 验证list是否是null或空
*
* @author wangcen
* @date 2021/7/2 14:25
*/
public class ListCustomValid implements ConstraintValidator<ListNotEmpty, List> {
@Override
public void initialize(ListNotEmpty constraintAnnotation) {}
@Override
public boolean isValid(List value, ConstraintValidatorContext context) {
if(CollectionUtils.isNotEmpty(value)) {
return true;
}
return false;
}
}
使用
@ListNotEmpy(message=“xxx不能为空”)