Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation error: "No validator could be found for type: java.lang.Integer"

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

}
like image 953
SJS Avatar asked May 12 '11 18:05

SJS


4 Answers

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.

like image 71
Affe Avatar answered Nov 14 '22 20:11

Affe


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

like image 44
Nitin Jha Avatar answered Nov 14 '22 21:11

Nitin Jha


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.

like image 11
Thamaraikannan Rajendran Avatar answered Nov 14 '22 21:11

Thamaraikannan Rajendran


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.

like image 5
tej Avatar answered Nov 14 '22 20:11

tej