Java validation @min @max annotion for null values

Viewed 7182

I have put validation for the following field,

@Min(1)
@Max(500)
private int length;

however, the length isn't a required field but when I didn't give the "length" in the input, I got this error:

   "Validation error, message = must be greater than or equal to 1, path = length"

Looking at the @min and @max documentation, it says "null element is considered valid". I know that. If @min @max is only for primitive type, then why the documentation mentions "null" element is considered valid? Can someone let me know how to fix the validation problem? Many thanks.

1 Answers

For optional integer values, you may use Integer instead of int, since an int variable cannot be null and will have 0 as a default value. With an Integer, length will be null by default and you should be able to pass the validation.

Related