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

时间:2011-05-12 18:37:31

标签: java hibernate validation spring-mvc

我正在与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;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "ttl_d_sls_mtht", nullable = false)
    private Integer ttl_d_sls_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "ttl_s_sls_lst_mth", nullable = false)
    private Integer ttl_s_sls_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "ttl_s_sls_6_mth", nullable = false)
    private Integer ttl_s_sls_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "ttl_s_sls_mtht", nullable = false)
    private Integer ttl_s_sls_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "nw_lst_mth", nullable = false)
    private Integer nw_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "nw_6_mth", nullable = false)
    private Integer nw_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "nw_mtht", nullable = false)
    private Integer nw_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "usd_lst_mth", nullable = false)
    private Integer usd_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "usd_6_mth", nullable = false)
    private Integer usd_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "usd_mtht", nullable = false)
    private Integer usd_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvnr_lst_mth", nullable = false)
    private Integer grss_prt_pvnr_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvnr_6_mth", nullable = false)
    private Integer grss_prt_pvnr_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvnr_mtht", nullable = false)
    private Integer grss_prt_pvnr_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvur_lst_mth", nullable = false)
    private Integer grss_prt_pvur_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvur_6_mth", nullable = false)
    private Integer grss_prt_pvur_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvur_mtht", nullable = false)
    private Integer grss_prt_pvur_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_tffic_lst_mth", nullable = false)
    private Integer shwrm_tffic_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_tffic_6_mth", nullable = false)
    private Integer shwrm_tffic_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_tffic_mtht", nullable = false)
    private Integer shwrm_tffic_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_clsng_lst_mth", nullable = false)
    private Integer shwrm_clsng_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_clsng_6_mth", nullable = false)
    private Integer shwrm_clsng_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_clsng_mtht", nullable = false)
    private Integer shwrm_clsng_mtht;

    @Column(name = "date_added")
    private Date addedDate;


    public Integer getTtl_d_sls_lst_mth() {
        return ttl_d_sls_lst_mth;
    }

    public void setTtl_d_sls_lst_mth(Integer ttl_d_sls_lst_mth) {
        this.ttl_d_sls_lst_mth = ttl_d_sls_lst_mth;
    }

    public Integer getTtl_d_sls_6_mth() {
        return ttl_d_sls_6_mth;
    }

    public void setTtl_d_sls_6_mth(Integer ttl_d_sls_6_mth) {
        this.ttl_d_sls_6_mth = ttl_d_sls_6_mth;
    }

    public Integer getTtl_d_sls_mtht() {
        return ttl_d_sls_mtht;
    }

    public void setTtl_d_sls_mtht(Integer ttl_d_sls_mtht) {
        this.ttl_d_sls_mtht = ttl_d_sls_mtht;
    }

    public Integer getTtl_s_sls_lst_mth() {
        return ttl_s_sls_lst_mth;
    }

    public void setTtl_s_sls_lst_mth(Integer ttl_s_sls_lst_mth) {
        this.ttl_s_sls_lst_mth = ttl_s_sls_lst_mth;
    }

    public Integer getTtl_s_sls_6_mth() {
        return ttl_s_sls_6_mth;
    }

    public void setTtl_s_sls_6_mth(Integer ttl_s_sls_6_mth) {
        this.ttl_s_sls_6_mth = ttl_s_sls_6_mth;
    }

    public Integer getTtl_s_sls_mtht() {
        return ttl_s_sls_mtht;
    }

    public void setTtl_s_sls_mtht(Integer ttl_s_sls_mtht) {
        this.ttl_s_sls_mtht = ttl_s_sls_mtht;
    }

    public Integer getNw_lst_mth() {
        return nw_lst_mth;
    }

    public void setNw_lst_mth(Integer nw_lst_mth) {
        this.nw_lst_mth = nw_lst_mth;
    }

    public Integer getNw_6_mth() {
        return nw_6_mth;
    }

    public void setNw_6_mth(Integer nw_6_mth) {
        this.nw_6_mth = nw_6_mth;
    }

    public Integer getNw_mtht() {
        return nw_mtht;
    }

    public void setNw_mtht(Integer nw_mtht) {
        this.nw_mtht = nw_mtht;
    }

    public Integer getUsd_lst_mth() {
        return usd_lst_mth;
    }

    public void setUsd_lst_mth(Integer usd_lst_mth) {
        this.usd_lst_mth = usd_lst_mth;
    }

    public Integer getUsd_6_mth() {
        return usd_6_mth;
    }

    public void setUsd_6_mth(Integer usd_6_mth) {
        this.usd_6_mth = usd_6_mth;
    }

    public Integer getUsd_mtht() {
        return usd_mtht;
    }

    public void setUsd_mtht(Integer usd_mtht) {
        this.usd_mtht = usd_mtht;
    }

    public Integer getGrss_prt_pvnr_lst_mth() {
        return grss_prt_pvnr_lst_mth;
    }

    public void setGrss_prt_pvnr_lst_mth(Integer grss_prt_pvnr_lst_mth) {
        this.grss_prt_pvnr_lst_mth = grss_prt_pvnr_lst_mth;
    }

    public Integer getGrss_prt_pvnr_6_mth() {
        return grss_prt_pvnr_6_mth;
    }

    public void setGrss_prt_pvnr_6_mth(Integer grss_prt_pvnr_6_mth) {
        this.grss_prt_pvnr_6_mth = grss_prt_pvnr_6_mth;
    }

    public Integer getGrss_prt_pvnr_mtht() {
        return grss_prt_pvnr_mtht;
    }

    public void setGrss_prt_pvnr_mtht(Integer grss_prt_pvnr_mtht) {
        this.grss_prt_pvnr_mtht = grss_prt_pvnr_mtht;
    }

    public Integer getGrss_prt_pvur_lst_mth() {
        return grss_prt_pvur_lst_mth;
    }

    public void setGrss_prt_pvur_lst_mth(Integer grss_prt_pvur_lst_mth) {
        this.grss_prt_pvur_lst_mth = grss_prt_pvur_lst_mth;
    }

    public Integer getGrss_prt_pvur_6_mth() {
        return grss_prt_pvur_6_mth;
    }

    public void setGrss_prt_pvur_6_mth(Integer grss_prt_pvur_6_mth) {
        this.grss_prt_pvur_6_mth = grss_prt_pvur_6_mth;
    }

    public Integer getGrss_prt_pvur_mtht() {
        return grss_prt_pvur_mtht;
    }

    public void setGrss_prt_pvur_mtht(Integer grss_prt_pvur_mtht) {
        this.grss_prt_pvur_mtht = grss_prt_pvur_mtht;
    }

    public Integer getShwrm_tffic_lst_mth() {
        return shwrm_tffic_lst_mth;
    }

    public void setShwrm_tffic_lst_mth(Integer shwrm_tffic_lst_mth) {
        this.shwrm_tffic_lst_mth = shwrm_tffic_lst_mth;
    }

    public Integer getShwrm_tffic_6_mth() {
        return shwrm_tffic_6_mth;
    }

    public void setShwrm_tffic_6_mth(Integer shwrm_tffic_6_mth) {
        this.shwrm_tffic_6_mth = shwrm_tffic_6_mth;
    }

    public Integer getShwrm_tffic_mtht() {
        return shwrm_tffic_mtht;
    }

    public void setShwrm_tffic_mtht(Integer shwrm_tffic_mtht) {
        this.shwrm_tffic_mtht = shwrm_tffic_mtht;
    }

    public Integer getShwrm_clsng_lst_mth() {
        return shwrm_clsng_lst_mth;
    }

    public void setShwrm_clsng_lst_mth(Integer shwrm_clsng_lst_mth) {
        this.shwrm_clsng_lst_mth = shwrm_clsng_lst_mth;
    }

    public Integer getShwrm_clsng_6_mth() {
        return shwrm_clsng_6_mth;
    }

    public void setShwrm_clsng_6_mth(Integer shwrm_clsng_6_mth) {
        this.shwrm_clsng_6_mth = shwrm_clsng_6_mth;
    }

    public Integer getShwrm_clsng_mtht() {
        return shwrm_clsng_mtht;
    }

    public void setShwrm_clsng_mtht(Integer shwrm_clsng_mtht) {
        this.shwrm_clsng_mtht = shwrm_clsng_mtht;
    }

    public Date getAddedDate() {
        return addedDate;
    }

    public void setAddedDate(Date addedDate) {
        this.addedDate = addedDate;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Id
    @Column(name = "username")
    private String username;

}

5 个答案:

答案 0 :(得分:170)

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

  

public @interface NotEmpty

     

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

答案 1 :(得分:2)

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

答案 2 :(得分:2)

如问题所述,要解决此错误,您必须使用正确的注释。在上述问题中,@NotBlank@NotEmpty注释只能应用于任何String字段。

要验证长类型字段,请使用注释@NotNull

答案 3 :(得分:0)

您可以添加休眠验证器依赖项,以提供验证器

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

答案 4 :(得分:-1)

UnexpectedTypeException错误:如果我们试图在任何bean属性上使用不正确的Hibernate验证程序注释。 对于我的Springboot项目(验证类型为'java.lang.Integer')有相同的问题尝试使用@NotNull for Integer 为我工作