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

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.lang.Integer'. Check configuration for 'discounted'

My code is:

@Column(name= "discount_percentage")
@Size(min=0, max=90)
@Min(0)
@Max(90)
private Integer discountPercentage = 0;

I set it to 0 because i was getting a NullPointerException when loading my view. And is Integer because i was reading in others question, and some people says that sometimes there are problems when using @Size with primitive types.

What should i do? Thanks in advance.

@Size is a Bean Validation annotation that validates that the associated String has a value whose length is bounded by the minimum and maximum values. And as your exception says it does not apply to Integer type.

Use: @Range

@Column(name= "discount_percentage")
@Range(min=0, max=90)
private Integer discountPercentage = 0;

Or you cloud also use only @Max or @Min and that will work too. For more info please take a look on this link.

Note that @Range is found in org.hibernate.validator.constraints, and not in javax.validation.constraints – jumping_monkey Mar 24, 2020 at 9:40

@Size is not used to validate min/max.

It's used to validate size of collections, length of strings, etc.

In this case you should use @Min, @Max instead.

Refer here for complete document: https://docs.oracle.com/javaee/7/api/javax/validation/constraints/Size.html

I have observed this pattern when, the parameter type does not match with validator. as here List<String> is not matiching List<Long>

Ex Validator:

public class MaxValidation implements ConstraintValidator<MaxParameterSize, List<String>> {

Annotation Use :

    public Response abc(@NotNull @MaxParameterSize(10) List<Long> parcelIds { 
        

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.