4

我正在尝试使用自定义验证器验证 bean。但是验证器需要从调用 validate 的方法传递给它的信息。有没有办法做到这一点?

我无法在初始化中传递它,因为它在创建 bean 时不可用。

要验证的 Bean

class vehicle {
   @VehicleNameValidator
   String vehicleName;
}

注解

@Target({ElementType.FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = VehicleNameValidatorImpl.class)
@Documented
public @interface VehicleNameValidator {
    String message() default "Invalid vehicle Name";

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

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

自定义验证器

public class VehicleNameValidatorImpl implements ConstraintValidator<VehicleNameValidator, String[]> {

    @Override
    public boolean isValid(String vehicleName, ConstraintValidatorContext constraintValidatorContext) {
        boolean isValid = logicmethod()//some logic here
        return isValid;
    }
}

主要方法

public static void main(String[] args) {
   String whatIwantToPass = runtimelogic(args);
   vehicle veh1 = new vehicle();
   Set<ConstraintViolation<vehicle>> constraintViolations = 
   validator.validate(veh1);
}

如何将变量“whatIwantToPass”从主方法传递给 VehicleNameValidatorImpl。

4

2 回答 2

4

如果您使用的是 Hibernate Validator,您可能需要查看约束验证器有效负载。在您的情况下,它看起来有点像这样:

在您的验证器 impl 中,您可以访问有效负载:

public boolean isValid(String vehicleName, ConstraintValidatorContext constraintValidatorContext) {
    boolean isValid = false;
    if ( constraintValidatorContext instanceof HibernateConstraintValidatorContext ) {
        final String payload = constraintValidatorContext
                .unwrap( HibernateConstraintValidatorContext.class )
                .getConstraintValidatorPayload( String.class );
        // do whatever you need
        isValid = logicmethod()//some logic here
    }
    return isValid;
}

可以在验证器工厂级别或每个验证器上设置有效负载,如下所示:

ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
        .configure()
        .buildValidatorFactory();

String whatIwantToPass = runtimelogic( args );

Validator validator = validatorFactory.unwrap( HibernateValidatorFactory.class )
        .usingContext()
        .constraintValidatorPayload( whatIwantToPass )
        .getValidator();

// use the validator with the constraint validator payload 
vehicle veh1 = new vehicle();
Set<ConstraintViolation<vehicle>> constraintViolations = validator.validate( veh1 );

有关更多详细信息,请查看文档的这一部分 -将有效负载传递给约束验证器

于 2019-06-27T08:06:37.710 回答
0

你总是可以在实现中使用@Autowired 或@Value 注释。ConstraintValidator我相信你可以在你的VehicleNameValidatorImpl.class

    public class VehicleNameValidatorImpl implements ConstraintValidator<VehicleNameValidator, String[]> {

        @Autowired
        LogicWrittenClass logicWrittenClass;

        @Override
        public boolean isValid(String[] roles, ConstraintValidatorContext constraintValidatorContext) {
            String whatIwantToPass  = logicWrittenClass.runtimelogic();
            boolean isValid = logicmethod()//some logic here
            return isValid;
        }
    }

但是请确保您使用 spring 创建验证器 bean,否则 bean 注入将不起作用,因为使用验证工厂(Hibernate Validator 作为参考实现)手动创建验证器 bean 不会将依赖项注入到 ConstraintValidator 实例中。

于 2019-06-26T05:53:34.883 回答