将参数从验证传递到休眠验证器中的自定义验证器

Pass parameter from validate to custom validator in hibernate validator

我正在尝试使用自定义验证程序验证 bean。但是验证器需要从调用验证的方法传递给它的信息。有办法吗?

我无法在初始化中传递它,因为它在创建 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" 从 main 方法传递给 VehicleNameValidatorImpl。

您始终可以在 ConstraintValidator 的实现中使用 @Autowired 或 @Value 注释。我相信您可以在 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实例。

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

在您的验证器实现中,您可以访问负载:

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 );

有关更多详细信息,请查看文档的这一部分 - Passing a payload to the constraint validator