我想拆分我的验证器的声明和实现,与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验证以使用我的自定义验证注释的实现?
Hibernate Validator正在使用默认的implConstraintValidatorFactory
(请参阅stacktrace中的ConstraintValidatorFactoryImpl
),这意味着您的自定义impl没有被考虑在内。
方法验证失败了,也许你需要提供一个ExecutableValidator
bean?
(Hibernate验证器在这里领先,我不知道Spring是如何连接东西的)。
目前,不允许在接口中使用get/set方法(访问器)。例如: 此外,TypeScript 不允许在类方法中使用数组函数表达式:例如: 有没有其他方法可以在接口定义上使用getter和setter?
我的配置是:Wildfly8.2.0,Weld 是否可以在CDI中注入bean而不是在其接口中注入?
我有一个电子邮件应用程序发送电子邮件,是在家里写的。我们已经为它设置了使用OAuth2.0与GMail(个人和商业账户)和Outlook.com账户的选项,没有问题。 我们也可以使用用户ID和密码进行身份验证,但是我们更喜欢OAuth2.0,因为我们不会以这种方式将密码保存在任何地方。 我们现在要求为Office365帐户这样做。 我注意到Office365 smtp服务器(smtp.Office
我用这个包裹https://www.npmjs.com/package/vue-sweetalert2我想通过vue中的vue组件。以html的形式浏览。 但什么都没有。我是VueJs新手,我仍然不完全理解如何将组件作为html插入。
假设我有一个接口,它表示我的重置服务,使用
使用wsgen生成WSDL时,是否可以使用接口作为服务endpoint接口? 当我尝试这样做时,我得到一个错误:-类“com.nimble.sample.Calc”不是endpoint实现类。 CXF工作正常,但这个问题只有在使用JAX-WS时才会出现