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

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

问题描述

我想强调的是,我已经搜索了此类问题,但是找不到适合我的情况的解决方案.在我的Spring Boot Web应用程序中,使用包javax.validation.constraints的@NotEmpty或@NotBlank批注验证bean时,始终出现此错误:

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)

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

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>

我看到休眠验证器正在工作,因为如果我不使用任何@NotEmpty或@NotBlank批注,则其他批注(例如@Size)也可以正常工作.

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.

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

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

此版本与Maven解析的hibernate-validator 6.0.11 jar版本不同.

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.

推荐答案

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

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

这就是为什么我在启动日志中看到版本5.3.5.Final-redhat-2的原因,而不是在WAR中看到的版本.在以下位置还具有validation-api-1.1.0.Final-redhat-1.jar:

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

通过在Web应用程序的META-INF根目录中创建一个jboss-deployment-structure.xml文件并尝试使用打包在应用程序WAR/JAR中的文件,可以尝试排除这些依赖关系.

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.

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

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

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

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天全站免登陆