验证错误:“找不到类型的验证器:java.lang.Integer”
我正在使用 Spring 开发一个项目,为什么我不断收到以下错误?
javax.validation.UnexpectedTypeException:
找不到类型的验证器:java.lang.Integer
这是我的代码:
package com.s2rsolutions.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
@Entity
@Table(name = "sales")
public class Sales {
@NotEmpty(message = "The above field must not be blank.")
@Column(name = "ttl_d_sls_lst_mth", nullable = false)
private Integer ttl_d_sls_lst_mth;
@NotEmpty(message = "The above field must not be blank.")
@Column(name = "ttl_d_sls_6_mth", nullable = false)
private Integer ttl_d_sls_6_mth;
@Column(name = "date_added")
private Date addedDate;
@Id
@Column(name = "username")
private String username;
// other fields/getters/setters omitted for brevity
}
I am working on a project with Spring why do I keep getting the following error?
javax.validation.UnexpectedTypeException:
No validator could be found for type: java.lang.Integer
Here is my code:
package com.s2rsolutions.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
@Entity
@Table(name = "sales")
public class Sales {
@NotEmpty(message = "The above field must not be blank.")
@Column(name = "ttl_d_sls_lst_mth", nullable = false)
private Integer ttl_d_sls_lst_mth;
@NotEmpty(message = "The above field must not be blank.")
@Column(name = "ttl_d_sls_6_mth", nullable = false)
private Integer ttl_d_sls_6_mth;
@Column(name = "date_added")
private Date addedDate;
@Id
@Column(name = "username")
private String username;
// other fields/getters/setters omitted for brevity
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据 NotEmpty 的 javadoc,整数不是要检查的有效类型。它适用于字符串和集合。如果您只是想确保 Integer 有一定的值,那么 javax.validation.constraints.NotNull 就足够了。
As per the javadoc of NotEmpty, Integer is not a valid type for it to check. It's for Strings and collections. If you just want to make sure an Integer has some value,
javax.validation.constraints.NotNull
is all you need.正如问题中所述,要解决此错误,您必须使用正确的注释。在上述问题中,
@NotBlank
或@NotEmpty
注释必须仅应用于任何字符串字段。要验证长类型字段,请使用注释
@NotNull
。As stated in problem, to solve this error you MUST use correct annotations. In above problem,
@NotBlank
or@NotEmpty
annotation must be applied on any String field only.To validate long type field, use annotation
@NotNull
.当问题被问到时,只需在整数字段上使用 @Min(1) 而不是 @size 即可。
As the question is asked simply use @Min(1) instead of @size on integer fields and it will work.
对于此类型错误:UnexpectedTypeException 错误:我们尝试在任何 bean 属性上使用不正确的 Hibernate 验证器注释。对于我的 Springboot 项目(验证类型“java.lang.Integer”)的同一问题,
对我有用的解决方案是使用
@NotNull
来表示 Integer。For this type error: UnexpectedTypeException ERROR: We are trying to use incorrect Hibernate validator annotation on any bean property. For this same issue for my Springboot project( validating type 'java.lang.Integer')
The solution that worked for me is using
@NotNull
for Integer.您可以添加 hibernate validator 依赖项,以提供 Validator
You can add hibernate validator dependency, to provide a Validator