使用@NotEmpty验证Scala的Map时抛出javax.validation.UnexpectedTypeException:HV000030异常

时间:2018-01-19 05:29:17

标签: java scala hibernate validation hibernate-validator

当我尝试验证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的地图以检查它是否为空?

2 个答案:

答案 0 :(得分:1)

所以你要理解的是,在Hibernate Validator中,对于给定的约束,你为不同的类型注册约束验证器。

通常,对于@Size约束,我们将为String,Collection,Map,arrays注册约束验证器......

您遇到的问题是Scala类型没有实现传统的Java接口,因此我们找不到它们的约束验证器。这就是你得到异常的原因:HV识别约束但无法找到约束验证器将其应用于Scala Map。

就我从代码中看到的那样,https://github.com/bean-validation-scala/bean-validation-scala将成为获得Scala的BV和HV支持的方法。它将验证委托给原始的HV实现,因此您应该具有与Java相同的行为。

我不认为它已经过时,因为请记住BV / HV项目暂时没有太大变化,在我们开始研究BV 2.0和HV 6.0之前。关于BV 2.0和HV 6.0支持(我们有新的约束等等)对原作者进行ping操作可能是一个好主意。

答案 1 :(得分:0)

如果您想检查地图集合是否为空,您可以执行此操作:if(mapVariable.isEmpty() || mapVariable == null){...}

或者按照第二个链接(链接到API)中的说明编写traversable shouldBe emptyjavaMap should not be empty。查看thisthis了解详情。

修改

尝试将@Valid添加到集合中。

以下是Hibernate Validator Reference

中的示例
public class Car { 
@NotNull 
@Valid 
private List<Person> passengers = new ArrayList<Person>(); 
}

这是标准的JSR-303行为。参见spec的第3.1.3节。