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.
@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.
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.
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.
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With