SpringBoot 项目中使用的自定义注解

93 阅读1分钟
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 如果指定的字段(fieldName)等于任何一个值(fieldValue)时,被验证的字段(dependFieldName)必须等于另一个值(dependFieldValue)
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = AcceptedIfValidationImpl.class)
public @interface AcceptedIf {
    /**
     * 指定字段
     * @return
     */
    String fieldName();

    /**
     * 指定字段值
     * @return
     */
    String fieldValue();

    /**
     * 被校验字段
     * @return
     */
    String dependFieldName();

    /**
     * 被校验字段值
     * @return
     */
    String dependFieldValue();

    /**
     * 错误提示
     */
    String message() default "";

    /**
     * 分组校验
     */
    Class<?>[] groups() default {};

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

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @interface List {
        AcceptedIf[] value();
    }
}

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.ValidationException;
import java.lang.reflect.InvocationTargetException;


public class AcceptedIfValidationImpl implements ConstraintValidator<AcceptedIf, Object> {
    private String fieldName;
    private String fieldValue;
    private String dependFieldName;
    private String dependFieldValue;
    private String message;

    @Override
    public void initialize(AcceptedIf constraintAnnotation) {
        this.fieldName        = constraintAnnotation.fieldName();
        this.fieldValue       = constraintAnnotation.fieldValue();
        this.dependFieldName  = constraintAnnotation.dependFieldName();
        this.dependFieldValue = constraintAnnotation.dependFieldValue();
        this.message = constraintAnnotation.message();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext ctx) {

        if (value == null) {
            return true;
        }

        try {
            String fieldRealValue = BeanUtils.getProperty(value, this.fieldName);
            if (!this.fieldValue.equals(fieldRealValue)) {
                return true;
            }

            String dependRealValue = BeanUtils.getProperty(value, this.dependFieldName);
            if (this.dependFieldValue.equals(dependRealValue)) {
                return true;
            }

            if (StringUtils.isBlank(this.message)) {
                // 禁用默认提示信息
                ctx.disableDefaultConstraintViolation();
                // 设置提示语
                ctx.buildConstraintViolationWithTemplate(
                        this.fieldName + "字段值为:" + this.fieldValue + "时, "+ this.dependFieldName + "必须等于" + this.dependFieldValue
                ).addConstraintViolation();
            }
            return false;
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
            throw new ValidationException(ex);
        }
    }
}