【问题标题】:[HV000030: No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.lang.String'[HV000030:找不到约束“javax.validation.constraints.NotEmpty”验证类型“java.lang.String”的验证器
【发布时间】:2019-03-07 14:17:48
【问题描述】:

我想强调一下,我已经搜索过此类问题,但找不到适合我的案例的解决方案。 在我的 Spring Boot webapp 中,使用包 javax.validation.constraints 的 @NotEmpty 或 @NotBlank 注释验证 bean 时,我不断收到此错误:

14:04:59,426 ERROR [org.springframework.boot.web.servlet.support.ErrorPageFilter] (default task-33) Forwarding to error page from request [/registrati
on] due to exception [HV000030: No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.lang.String'.
 Check configuration for 'username']: javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation
.constraints.NotEmpty' validating type 'java.lang.String'. Check configuration for 'username'
        at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.throwExceptionForNullValidator(ConstraintTree.java:229)
        at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:310)
        at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(Constraint
Tree.java:244)

如果我使用@NotNull 注释不会出错,但这不是所需的行为,因为它允许空白字段。 这些是我的 pom.xml 依赖项:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
          <groupId>nz.net.ultraq.thymeleaf</groupId>
          <artifactId>thymeleaf-layout-dialect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-batch</artifactId>
       </dependency>
       <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/net.sourceforge.nekohtml/nekohtml -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.21</version><!--$NO-MVN-MAN-VER$-->
        </dependency>

        <!-- webjars -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.1.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.webjars</groupId>
                    <artifactId>jquery</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.webjars/datatables -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>datatables</artifactId>
            <version>1.10.19</version>
            <exclusions>
                <exclusion>
                    <groupId>org.webjars</groupId>
                    <artifactId>jquery</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <!-- plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin -->
        </plugins>
    </build>


</project>

我看到休眠验证器正在工作,因为如果我不使用任何 @NotEmpty 或 @NotBlank 注释,其他注释(如 @Size)将正常工作。

在我的 bean 中,我正在导入 javax.validation.constraints。 启动我的 JBoss 时,出现以下关于休眠验证器的行:

14:04:17,676 INFO  [org.hibernate.validator.internal.util.Version] (background-preinit) HV000001: Hibernate Validator 5.3.5.Final-redhat-2

这与 Maven 解析的 hibernate-validator 6.0.11 jar 版本不同。

发生了什么事?也许一些依赖冲突?

感谢所有可以帮助我的人。

【问题讨论】:

  • 我正在将我的应用程序部署到实现 Java EE7 规范的 Wildfly 10.1。我正在使用 spring-boot-starter-parent 2.1.1 版和 spring-boot-start-web。最后一个引入了 bean 验证 api 2.0,它是一个 Java EE8 规范。我想知道这是否可能是原因。我仍然在搞乱我的环境。看到这个:stackoverflow.com/questions/50751653/…

标签: hibernate validation annotations


【解决方案1】:

我遇到了类似的错误,但 ...Constraints.NotEmpty' validating type 'java.lang.Long'

这是因为我在非字符串字段中使用了@NotEmpty

我解决了它用@Size(value=1, message= "whatever")替换@NotEmpty

【讨论】:

  • 根据错误。消息,作者在字符串上使用@NotEmpty。
【解决方案2】:
  1. @NotBlank :检查注解的字符序列不为空,且修剪后的长度大于0。
  2. @NotNull :检查注解的值是否不为空,但可以为空。
  3. @NotEmpty :检查被注解的元素是否不为 null 也不为空。

在使用上述注解之前,必须考虑一下它是如何工作的。

如需进一步阅读,请使用这篇文章:Read more article oneRead more article two

【讨论】:

  • 根据错误。消息,作者在字符串上使用@NotEmpty。
【解决方案3】:

我也有同样的问题;我的 spring-boot-starter-web 依赖项包括对 org.hibernate:hibernate-validator:5.3.6.Final 的依赖项,但我将此排除项添加到我的 POM现在一切都很好:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-validator</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>

【讨论】:

  • 不幸的是,从 spring-boot-starter-web 中排除 hibernate-validator 依赖会导致我的 bean 类出现编译错误,因为 import javax.validation.constraints.* 没有解决。如果除了上述步骤之外,我还添加了验证 API,则会收到错误“找不到 Bean 验证提供程序”。我可以让它工作的唯一方法是将 Hibernate 验证器依赖项保留在 spring-boot-starter-web 和我的 User bean 类 import org.hibernate.validator.constraints.* 中,即使它们已被弃用。我真的无法理解我做错了什么。请帮忙...
  • 如果你排除了hibernate-validator依赖,你需要使用org.hibernate.validator.constraints.NotEmpty而不是javax.validation.constraints.NotEmpty
  • @Simon 我和你有同样的问题。您能解决它还是坚持使用已弃用的导入?
  • @ChuckL 不幸的是,我没有时间进一步调查这个问题。我仍在使用已弃用的导入。如果您找到解决方案,请更新此帖子。如果我有时间,我也会这样做。
  • 感谢您的回答!在我的例子中,依赖树显示 hibernate-validator 是我的许多其他依赖项的传递依赖项。从所有这些中排除后,使用 javax.validation.constraints.NotEmpty 按预期工作。
【解决方案4】:

感谢 ChuckL 的评论,我刚刚发现 JBoss 7.1 在下面有自己的 hibernate-validator-5.3.5.Final-redhat-2.jar:

modules\system\layers\base\org\hibernate\validator\main

这就是为什么我在启动日志中看到版本 5.3.5.Final-redhat-2 而不是我的 WAR 中打包的版本。下还有validation-api-1.1.0.Final-redhat-1.jar:

modules\system\layers\base\javax\validation\api\main

可以尝试通过在 webapp 的根 META-INF 中创建 jboss-deployment-structure.xml 文件并尝试使用打包在应用程序 WAR/JAR 中的那些来排除这些依赖项。

无论如何,JBoss 7.1 是一个 JEE7 实现容器。更改 JBoss 附带的 bean 验证模块可能会导致我不想尝试的意外后果 :)

干净的解决方案是将 JBoss 迁移到支持 JEE8 的较新版本,否则继续使用已弃用的 API。

【讨论】:

  • 感谢@Simon 的更新!是的,我一直想提交一个答案,但你已经涵盖了所有内容。我安装了 Wildfly 15,部署了我的应用程序,一切正常,没有错误。 :-) 所以就像你说的,要么继续使用已弃用的 API,要么升级到实现 JEE8 规范的服务器版本。
【解决方案5】:

在 pom.xml 中添加更新版本修复了这个错误:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.1.6.Final</version>
    </dependency>

我遇到了这个错误,因为我直接使用了另一个版本的验证器 api。像这样:

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
        <scope>compile</scope>
    </dependency>

【讨论】:

    【解决方案6】:

    您的休眠验证器正在实现 Java Bean Validation 1.1 规范,但 @NotBlank 来自 Java Bean Validation 2.0。因此,您有三个选择:

    1. 升级您的休眠验证器,使其实现 Java Bean Validation 2.0 规范
    2. 删除@NotBlank,因为您的休眠验证器版本不支持它。
    3. 删除休眠验证器并丢失 bean 验证功能,但此错误将消失。

    【讨论】:

      猜你喜欢
      • 2021-02-04
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 2023-01-05
      相关资源
      最近更新 更多