我想强调的是,我已经搜索了此类问题,但找不到适合我的案例的解决方案。
在我的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>
我看到 hibernate 验证程序正在工作,因为如果我不使用任何@NotEmpty或@NotBlank批注,则其他批注(例如@Size)也可以正常工作。
在我的bean中,我正在导入javax.validation.constraints。
启动我的JBoss时,出现以下有关 hibernate validator 的代码:
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版本不同。
发生了什么?也许有些依赖冲突?
感谢所有可以帮助我的人。
最佳答案
我遇到类似的错误,但...Constraints.NotEmpty' validating type 'java.lang.Long'
发生这种情况是因为我在非字符串字段中使用了@NotEmpty
。
我解决了用@NotEmpty
替换@Size(value=1, message= "whatever")
的问题