2

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Vignesh_A
  • 508
  • 1
  • 7
  • 19
  • 1
    This is going to be a problem at some point no matter what; you might as well let the parse error bubble up. Otherwise, you could use a `Long` in your DTO. – chrylis -cautiouslyoptimistic- Aug 10 '19 at 17:58
  • 1
    Even long also have a limited range maximum- '9,223,372,036,854,775,807' . If OP wants to control the validation message, better do it as BigInteger or String – nagendra547 Aug 10 '19 at 18:14

1 Answers1

2

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

Turac
  • 667
  • 1
  • 4
  • 19