验证错误:“找不到类型的验证器:java.lang.Integer”

发布于 2024-11-06 10:47:25 字数 1028 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

季末如歌 2024-11-13 10:47:25

根据 NotEmpty 的 javadoc,整数不是要检查的有效类型。它适用于字符串和集合。如果您只是想确保 Integer 有一定的值,那么 javax.validation.constraints.NotNull 就足够了。

公共@interfaceNotEmpty

断言带注释的字符串,
集合、映射或数组不为空
或为空。

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.

public @interface NotEmpty

Asserts that the annotated string,
collection, map or array is not null
or empty.

岁月静好 2024-11-13 10:47:25

正如问题中所述,要解决此错误,您必须使用正确的注释。在上述问题中,@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.

三岁铭 2024-11-13 10:47:25

当问题被问到时,只需在整数字段上使用 @Min(1) 而不是 @size 即可。

As the question is asked simply use @Min(1) instead of @size on integer fields and it will work.

安静被遗忘 2024-11-13 10:47:25

对于此类型错误: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.

南风几经秋 2024-11-13 10:47:25

您可以添加 hibernate validator 依赖项,以提供 Validator

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.12.Final</version>
</dependency>

You can add hibernate validator dependency, to provide a Validator

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.12.Final</version>
</dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文