在Oracle的文档中,它说@Min用于检查“字段或属性的值必须是大于或等于值元素中的数字的整数值”。我使用 @Min(0) 验证表单输入为正整数或零,但在输入 1.2 时遇到 NumberFormatException。 (我使用Spring Boot + Thymeleaf)
我做错了什么?
我的代码:
@Min(value = 0, message = "need zero or positive integer")
private int qty;
当我输入-1时,我得到了我想要的消息,但是当我输入1.2时,我得到了NumberFormatException:
Failed to convert property value of type java.lang.String to required type int for property qty; nested exception is java.lang.NumberFormatException: For input string: "1.2"
最佳答案
您可以像这样验证数字。
import javax.validation.constraints.NotNull;
import javax.validation.constraints.PositiveOrZero;
@NotNull(message = "Prop must be not null.")
@PositiveOrZero(message = "Prop value should zero or positive.")
private Integer prop;
关于java - 使用@Min(0)验证整数遇到NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62113551/