gpt4 book ai didi

java - 抛出 javax.validation.UnexpectedTypeException : HV000030 exception when validating Scala's Map with @NotEmpty

转载 作者:行者123 更新时间:2023-12-02 11:43:57 27 4
gpt4 key购买 nike

当我尝试验证 Scala 的 Map 时,无论可变还是不可变,我都会遇到如下异常:

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'scala.collection.mutable.Map<java.lang.String, java.lang.String>'. Check configuration for 'corporationSecrets'
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.throwExceptionForNullValidator(ConstraintTree.java:229) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:310) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(ConstraintTree.java:244) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:163) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:116) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateComposingConstraints(ConstraintTree.java:398) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:98) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:87) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:73) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:621) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:584) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:528) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:496) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:461) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:411) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:208) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:102) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.validation.DataBinder.validate(DataBinder.java:877) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:274) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:330) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
... 29 common frames omitted

我的代码很简单,就像

@NotEmpty
val corporationSecrets : scala.collection.mutable.Map[String, String] = new scala.collection.mutable.HashMap[String, String]

如何验证 scala 的映射以检查它是否为空?

最佳答案

所以你必须明白的是,在 Hibernate Validator 中,对于给定的约束,你为不同类型注册约束 validator 。

通常,对于 @Size 约束,我们将为 String、Collection、Map、数组...注册约束 validator

这里的问题是 Scala 类型没有实现经典的 Java 接口(interface),因此我们找不到它们的约束 validator 。这就是为什么您会遇到异常:HV 识别约束,但找不到约束 validator 将其应用到 Scala 映射上。

据我从代码中可以看到,https://github.com/bean-validation-scala/bean-validation-scala将是获得 Scala 的 BV 和 HV 支持的方法。它将验证委托(delegate)给原始 HV 实现,因此您应该具有与 Java 所做的非常相似的行为。

我认为它并没有过时,因为请记住,在我们开始 BV 2.0 和 HV 6.0 的工作之前,BV/HV 项目有一段时间没有发生太大变化。向原作者询问 BV 2.0 和 HV 6.0 支持可能是个好主意(我们有新的限制等)。

关于java - 抛出 javax.validation.UnexpectedTypeException : HV000030 exception when validating Scala's Map with @NotEmpty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48334644/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com