当前位置: 首页 > 知识库问答 >
问题:

是否可以使用接口作为validatedBy

武峻熙
2023-03-14

我想拆分我的验证器的声明和实现,与Spring boot环境中的这个问题非常相似。看起来好像是我让它几乎起作用了。我看到我的验证器实际上是由Spring验证调用的,但在执行验证后,Hibernate会抛出一个异常


java.lang.NoSuchMethodException: test.UniqueUsernameValidator.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3427) ~[na:na]
    at java.base/java.lang.Class.getConstructor(Class.java:2165) ~[na:na]
    at org.hibernate.validator.internal.util.privilegedactions.NewInstance.run(NewInstance.java:41) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorFactoryImpl.run(ConstraintValidatorFactoryImpl.java:43) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorFactoryImpl.getInstance(ConstraintValidatorFactoryImpl.java:28) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ClassBasedValidatorDescriptor.newInstance(ClassBasedValidatorDescriptor.java:84) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.AbstractConstraintValidatorManagerImpl.createAndInitializeValidator(AbstractConstraintValidatorManagerImpl.java:89) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManagerImpl.getInitializedValidator(ConstraintValidatorManagerImpl.java:117) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:136) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.validateConstraints(SimpleConstraintTree.java:54) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:75) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.doValidateConstraint(MetaConstraint.java:130) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:123) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:555) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:518) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:488) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:450) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:400) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedAnnotatedObjectForCurrentGroup(ValidatorImpl.java:629) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedConstraints(ValidatorImpl.java:590) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateParametersInContext(ValidatorImpl.java:880) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:283) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:235) ~[hibernate-validator-6.1.5.Final.jar:6.1.5.Final]
    at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:104) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at test.AuthenticationController$$EnhancerBySpringCGLIB$$f2b10b56.signUp(<generated>) ~[main/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
    ...
    

这是因为UniqueUsernameValidator是一个接口(如预期)。

我已经这样配置了Spring(这是一个不同问题的答案):

  @Bean
  public Validator validator() {
    SpringConstraintValidatorFactoryEx scvf =
        new SpringConstraintValidatorFactoryEx(wac.getAutowireCapableBeanFactory());

    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.setConstraintValidatorFactory(scvf);
    validator.setApplicationContext(wac);
    validator.afterPropertiesSet();
    return validator;
  }

我的自定义验证器:

@Component
@Slf4j
public class SpringConstraintValidatorFactoryEx implements ConstraintValidatorFactory {

  private AutowireCapableBeanFactory beanFactory;

  public SpringConstraintValidatorFactoryEx(AutowireCapableBeanFactory beanFactory) {
    super();
    this.beanFactory = beanFactory;
  }

  public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
    T bean = null;

    try {
      log.info("Trying to find a validator bean of class " + key.getSimpleName());
      bean = (T) this.beanFactory.getBean(key);
    } catch (BeansException exc) {
      log.info(
          "Failed to find a bean of class {}, message {}", key.getSimpleName(), exc.getMessage());
    }

    if (bean == null) {
      try {
        log.info("Creating a new validator bean of class " + key.getSimpleName());
        bean = this.beanFactory.createBean(key);
      } catch (BeansException exc) {
        log.info("Failed to create a validator of class " + key.getSimpleName());
      }
    }

    if (bean == null) {
      log.warn("Failed to get validator of class " + key.getSimpleName());
    }

    return bean;
  }

  @Override
  public void releaseInstance(ConstraintValidator<?, ?> instance) {}
}

所以它试图通过验证器名称找到一个Spring bean。所以我有一个验证器的实现:

@Component
public class UniqueUsernameValidatorImpl implements UniqueUsernameValidator {
  @Autowired private UserCredentialsRepository repository;

  @Override
  public boolean isValid(String value, ConstraintValidatorContext context) {
    return repository.findByUsername(value).isEmpty();
  }
}

验证程序接口:

public interface UniqueUsernameValidator extends ConstraintValidator<UniqueUsername, String> {

}

约束注释:

@Target({
  ElementType.METHOD,
  ElementType.FIELD,
  ElementType.ANNOTATION_TYPE,
  ElementType.CONSTRUCTOR,
  ElementType.PARAMETER,
  ElementType.TYPE_USE
})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(UniqueUsername.List.class)
@Documented
@Constraint(validatedBy = {UniqueUsernameValidator.class})
public @interface UniqueUsername {
  String message() default "username must be unique";

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

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

  @Target({
    ElementType.METHOD,
    ElementType.FIELD,
    ElementType.ANNOTATION_TYPE,
    ElementType.CONSTRUCTOR,
    ElementType.PARAMETER,
    ElementType.TYPE_USE
  })
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  @interface List {
    UniqueUsername[] value();
  }
}

看起来Spring验证以某种方式成功地使用了我的验证器实现,但是HiberNate无法实例化验证器(?)。我不完全明白为什么有两种不同的验证机制初始化。

有没有办法“注册”或“通知”Hibernate验证以使用我的自定义验证注释的实现?

共有1个答案

沈永贞
2023-03-14

Hibernate Validator正在使用默认的implConstraintValidatorFactory(请参阅stacktrace中的ConstraintValidatorFactoryImpl),这意味着您的自定义impl没有被考虑在内。

方法验证失败了,也许你需要提供一个ExecutableValidatorbean?

(Hibernate验证器在这里领先,我不知道Spring是如何连接东西的)。

 类似资料: