Search code examples
javabean-validation

How do I validate an integer field in a Request bean, if the entered value is greater than the range of an int?


To validate a Integer field in Request bean, I used @range (min=0,max=99999999,message="invalid") and @digits(). they are throwing the error

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Numeric value (1111118493411) out of range of int;
  1. My need is to validate a Integer field and throw validation error in the request layer itself.
  2. my db has a field with size 10 which of type int4.
  3. I don't want user to pass value greater than 10digits.

How do I handle this in my request layer itself to restrict the user from entering more than 10 digits

@JsonProperty(value = "qty", required = true)
@NotNull
@Range(min=0, max=999999999 , message = "invalid")
private Integer qty;

I wish to throw an error stating invalid if the user enters more than 10 digits.


Solution

  • Max int value is 2,147,483,647 in java. You can see in this answer. Your value is bigger than max int value. So you should change qty's class field type(example long).