【问题标题】:Bean Validation update message relating to error与错误相关的 Bean Validation 更新消息
【发布时间】:2017-09-28 16:34:50
【问题描述】:

我有以下模型类

 public class ContactDetail {
     @NotNull(message="StartNum cannot be null")
     private int startNum;
     @NotNull(message="EndNum cannot be null")
     private int endNum; }

我将如何验证这些字段的范围大小? 我已经尝试添加

public int maxRangeSize=1000;

@AssertTrue(message="Range size is invalid" )
public boolean isRangeValid(){

Integer rangeSize= endNum-startNum;

if(rangeSize < 0 || rangeSize >= maxRangeSize)
{
    return false;
}
return true;

}

但我想知道是否有办法在验证失败消息中显示哪个 startnum 和 endnum 甚至设置的 maxrangesize 是什么,所以该错误对用户有用吗?

【问题讨论】:

    标签: java logging error-handling bean-validation


    【解决方案1】:

    试试这个例子:

    @Digits(integer = 3, fraction = 0, message = "The value of age can not be more than 3 digits")
    @Range(min = 10, max = 150)
    private int age;
    

    【讨论】:

    • 嗨,我不确定这对于查找两个 int 值之间的范围有什么用处,因为这只是看一个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 2011-11-15
    • 2022-06-25
    相关资源
    最近更新 更多