Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No validator could be found for constraint 'javax.validation.constraints.Size'

I have the following error

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.

like image 810
Lucas. D Avatar asked May 03 '18 17:05

Lucas. D


People also ask

Can we use @NotBlank for long?

@NotEmpty: a constrained CharSequence, Collection, Map, or Array is valid as long as it's not null, and its size/length is greater than zero. @NotBlank: a constrained String is valid as long as it's not null, and the trimmed length is greater than zero.

How do I validate a body request in spring boot?

Spring offers an elegant way to validate the user input. The @RequestBody annotation is used to bind the HTTP request body with a domain object in the method parameter and also this annotation internally uses the HTTP Message converter to convert the body of the HTTP request to a domain object.

What is Hibernate Validator framework?

Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming.


1 Answers

@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.

like image 58
Amit Bera Avatar answered Sep 19 '22 14:09

Amit Bera