首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用@Min(0)验证整数遇到的NumberFormatException

使用@Min(0)验证整数遇到的NumberFormatException
EN

Stack Overflow用户
提问于 2020-05-31 16:32:14
回答 1查看 263关注 0票数 0

我使用@Min(0)来验证表单输入是否为正整数或0,但在输入1.2时遇到了NumberFormatException。(我使用Spring Boot + Thymeleaf)

我做错了什么?

我的代码:

代码语言:javascript
复制
@Min(value = 0, message = "need zero or positive integer")
private int qty;

当我输入-1时,我得到了我想要的消息,但是当我输入1.2时,我得到了NumberFormatException:

代码语言:javascript
复制
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"
EN

回答 1

Stack Overflow用户

发布于 2020-05-31 16:42:39

代码语言:javascript
复制
import javax.validation.constraints.NotNull;
import javax.validation.constraints.PositiveOrZero;
代码语言:javascript
复制
@NotNull(message = "Prop must be not null.")
@PositiveOrZero(message = "Prop value should zero or positive.")
private Integer prop;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62113551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档