找不到约束“javax.validation.constraints.NotBlank”验证类型“java.lang.String”的验证器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52878299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.String'
提问by Fede Jinich
I'm using Hibernate validator to validate my beans.
我正在使用 Hibernate 验证器来验证我的 bean。
Actually I have this dependencies on my POM
实际上我对我的 POM 有这种依赖性
<!-- Hibernate validator - Bean validation API Implementation -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.11.Final</version>
</dependency>
<!-- Verify validation annotations usage at compile time -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.0.11.Final</version>
</dependency>
<!-- Unified Expression Language - Spec -->
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.1-b06</version>
</dependency>
<!-- Unified Expression Language - Implementation -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
And this a pice of my bean
这是我的豆子
import org.hibernate.annotations.ColumnDefault;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
public class Casa {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
@Column(name="nomcaac") @NotBlank(message = "{casa.nomcaac.null}") @Size(min = 0, max = 300, message = "{casa.nomcaac.size}")
private String nomcaac;
Now the problem comes when i try to validate my bean, i get this error message
现在问题来了,当我尝试验证我的 bean 时,我收到此错误消息
No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.String'. Check configuration for 'nomcaac'
找不到约束“javax.validation.constraints.NotBlank”验证类型“java.lang.String”的验证器。检查“nomcaac”的配置
I can't figure out whats my problem, I think I have the right dependencies according to this page.
我不知道我的问题是什么,我认为根据此页面我有正确的依赖项。
采纳答案by Guillaume Smet
@NotBlank
is new in Bean Validation 2.0 so I suspect you have an old Hibernate Validator version (e.g. 5.x) in your classpath somehow.
@NotBlank
在 Bean Validation 2.0 中是新的,所以我怀疑你的类路径中有一个旧的 Hibernate Validator 版本(例如 5.x)。
You should check your dependency tree with mvn dependency:tree
(and your webapp looking for an old jar).
你应该检查你的依赖树mvn dependency:tree
(和你的 webapp 寻找一个旧的 jar)。
By the way, your javax.el dependencies are incorrect: please refer to https://github.com/hibernate/hibernate-validator/#using-hibernate-validatorfor the correct one.
顺便说一句,您的 javax.el 依赖项不正确:请参阅https://github.com/hibernate/hibernate-validator/#using-hibernate-validator以获取正确的依赖项。