添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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;
    @Column(name = "username")
    private String username;
    // other fields/getters/setters omitted for brevity
                Looks like this is similar to what I faced earlier, this post helped me. To customize the error messages from failing at the bind time. Look this link for more details on creating a MessageSource bean in your application context and create a messages.properties resource bundle :
– Kris
                May 12, 2011 at 19:02

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.

+1. @NotEmpty, being proprietary, should be avoided altogether. It can be replaced by @NotNull, @Range and @Size (depending on context) – ChssPly76 May 12, 2011 at 20:43 Agree on avoiding vendor specific features when possible, but it is to its credit Free and Open Source :) – Affe Jul 12, 2012 at 21:08 Amazing how easy it is to unintentionally annotate an integer field with a @NotEmpty annotation. And then you get the less than helpful warning “No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.lang.Integer'.” – Michael Piefel Feb 6, 2017 at 15:35 What would be excellent is if there is a way of being able to get a more verbose error message to understand which property caused the issue for the exception thrown. If anyone knows this I would be very glad to know the answer. Thanks for the post it solved my problem. – Beezer Oct 4, 2017 at 14:22 @Beezer ConstraintViolationException has an additional method getConstraintViolations() that returns the list of ConstraintViolation objects that then each have a message. – Affe Oct 4, 2017 at 15:59

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.

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.

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.