Validate至少有一个不能为空

自定义过滤标签

import org.apache.commons.beanutils.PropertyUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * 验证至少有一个不能为空
 *
 * @author admin
 */
@Target({TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = AtLeastOneNotNull.AtLeastOneNotNullValidator.class)
@Documented
public @interface AtLeastOneNotNull {

    String message() default "at least one not null";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    String[] fieldNames();

    class AtLeastOneNotNullValidator implements ConstraintValidator<AtLeastOneNotNull, Object> {

        private String[] fieldNames;

        @Override
        public void initialize(AtLeastOneNotNull constraintAnnotation) {
            this.fieldNames = constraintAnnotation.fieldNames();
        }

        @Override
        public boolean isValid(Object object, ConstraintValidatorContext constraintContext) {

            if (object == null) {
                return true;
            }
            try {
                for (String fieldName : fieldNames) {
                    Object property = PropertyUtils.getProperty(object, fieldName);

                    if (property != null) return true;
                }
                return false;
            } catch (Exception e) {
                return false;
            }
        }
    }
}

应用

@Data
@ToString(callSuper = true)
@AtLeastOneNotNull(fieldNames = {"telphone", "phone"}, message = "telphone,phone不能都为空")
public class User {
    private String name;

    /**
     * 电话号码
     */
    private String telphone;

    /**
     * 手机号码
     */
    private String phone;
}
posted @ 2021-08-11 19:39  是谁扭曲了时空  阅读(1056)  评论(0编辑  收藏  举报