如果输入的值大于 int 的范围,如何验证请求 Bean 中的整数字段?



为了验证请求 bean 中的整数字段,我使用了他们抛出错误@range (min=0,max=99999999,message="invalid") and @digits().

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Numeric value (1111118493411) out of range of int;
  1. 我需要验证一个整数字段并在请求层本身中抛出验证错误。
  2. 我的数据库有一个大小为 10 的字段,类型为 Int4。
  3. 我不希望用户传递大于 10 位的值。

如何在请求层本身中处理此问题以限制用户输入超过 10 位数字

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

如果用户输入超过 10 位数字,我希望抛出一个错误,指出无效。

maxint值在 java 中2,147,483,647。你可以在这个答案中看到。您的值大于最大整数值。所以你应该改变 qty 的类字段类型(示例长(。

最新更新