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

前端 未结 5 476
故里飘歌
故里飘歌 2021-02-04 23:06

I am working on a project with Spring why do I keep getting the following error?

javax.validation.UnexpectedTypeException:
No validator could be fou

相关标签:
5条回答
  • 2021-02-04 23:26

    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>
    
    0 讨论(0)
  • 2021-02-04 23:32

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

    0 讨论(0)
  • 2021-02-04 23:33

    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.

    0 讨论(0)
  • 2021-02-04 23:51

    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.

    0 讨论(0)
  • 2021-02-04 23:51

    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.

    0 讨论(0)
提交回复
热议问题