[HV000030:找不到约束'javax.validation.constraints.NotEmpty'验证类型'java.lang.String'的验证器 [英] [HV000030: No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.lang.String'

查看:56
本文介绍了[HV000030:找不到约束'javax.validation.constraints.NotEmpty'验证类型'java.lang.String'的验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

14:04:59,426 错误 [org.springframework.boot.web.servlet.support.ErrorPageFilter] (default task-33) 从请求转发到错误页面 [/registration] 由于异常 [HV000030:无法找到约束javax.validation.constraints.NotEmpty"验证类型java.lang.String"的验证器.检查用户名"的配置]:javax.validation.UnexpectedTypeException:HV000030:找不到约束javax.validation"的验证器.constraints.NotEmpty' 验证类型'java.lang.String'.检查用户名"的配置在 org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.throwExceptionForNullValidator(ConstraintTree.java:229)在 org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:310)在 org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(Constraint树.java:244)

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

 <父><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.RELEASE</version><relativePath/><!-- 从存储库中查找父级 --></父母><属性><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></属性><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></依赖><依赖><groupId>nz.net.ultraq.thymeleaf</groupId><artifactId>thymeleaf-layout-dialect</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><排除事项><排除><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></排除></排除项></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId></依赖><依赖><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><范围>提供</范围></依赖><依赖><groupId>org.mariadb.jdbc</groupId><artifactId>mariadb-java-client</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><范围>测试</范围></依赖><依赖><groupId>org.springframework.security</groupId><artifactId>spring-security-test</artifactId><范围>测试</范围></依赖><依赖><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId></依赖><!-- https://mvnrepository.com/artifact/net.sourceforge.nekohtml/nekohtml --><依赖><groupId>net.sourceforge.nekohtml</groupId><artifactId>nekohtml</artifactId><version>1.9.21</version><!--$NO-MVN-MAN-VER$--></依赖><!-- webjars --><依赖><groupId>org.webjars</groupId><artifactId>bootstrap</artifactId><version>4.1.3</version><排除事项><排除><groupId>org.webjars</groupId><artifactId>jquery</artifactId></排除></排除项></依赖><!-- https://mvnrepository.com/artifact/org.webjars/datatables --><依赖><groupId>org.webjars</groupId><artifactId>数据表</artifactId><version>1.10.19</version><排除事项><排除><groupId>org.webjars</groupId><artifactId>jquery</artifactId></排除></排除项></依赖></依赖项><构建><插件><!-- 插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin --></plugins></build></项目>

我看到 hibernate 验证器正在工作,因为如果我不使用任何 @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 版本不同.

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

感谢所有能帮助我的人.

解决方案

感谢 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

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

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

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

I want to underline that I already searched for this kind of problem but couldn't find a solution for my case. In my Spring Boot webapp I keep getting this error when validating beans using @NotEmpty or @NotBlank annotation of package javax.validation.constraints:

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)

No errors if I use @NotNull annotation, but this is not the desired behavior because it allows for blank fields. These are my pom.xml dependencies:

    <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>

I see that hibernate validator is working because if I don't use any @NotEmpty nor @NotBlank annotation, other annotations such as @Size are working correctly.

In my bean I'm importing javax.validation.constraints. When starting up my JBoss, following line about hibernate validator appears:

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

This is not the same version as the hibernate-validator 6.0.11 jar that is resolved by Maven.

What's happening? Maybe some dependency conflict?

Thanks to everyone that could help me.

解决方案

Thanks to ChuckL comment I just found out that JBoss 7.1 has its own hibernate-validator-5.3.5.Final-redhat-2.jar under:

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

That's why I was seeing version 5.3.5.Final-redhat-2 in my startup logs and not the one packaged in my WAR. It also has validation-api-1.1.0.Final-redhat-1.jar under:

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

One could try to exclude those dependencies by creating a jboss-deployment-structure.xml file in the root META-INF of the webapp and trying to use those packaged in the application WAR/JAR.

Anyway JBoss 7.1 is a JEE7 implementation container. Changing the bean validation modules that are shipped with JBoss could lead to unexpected consequences that I don't want to experiment :)

The clean solution would be to migrate JBoss to a newer version which supports JEE8, otherwise keep using deprecated APIs.

这篇关于[HV000030:找不到约束'javax.validation.constraints.NotEmpty'验证类型'java.lang.String'的验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆