JOH*_*HND 50 java validation spring annotations
我想要做一些域验证
在我的对象中,我有一个整数,
现在我的问题是如果我写
@Min(SEQ_MIN_VALUE)
@Max(SEQ_MAX_VALUE)
private Integer sequence;
Run Code Online (Sandbox Code Playgroud)
和
@Size(min = 1, max = NAME_MAX_LENGTH)
private Integer sequence;
Run Code Online (Sandbox Code Playgroud)
如果是整数哪一个适合域验证.
任何人都能解释一下他们之间的区别是什么?
谢谢.
Rah*_*wal 17
package com.mycompany;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Car {
@NotNull
private String manufacturer;
@NotNull
@Size(min = 2, max = 14)
private String licensePlate;
@Min(2)
private int seatCount;
public Car(String manufacturer, String licencePlate, int seatCount) {
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
this.seatCount = seatCount;
}
//getters and setters ...
}
Run Code Online (Sandbox Code Playgroud)
@NotNull,@Size并且@Min是所谓的约束注释,我们用它来声明约束,这些约束应该应用于Car实例的字段:
manufacturer 永远不会为空
licensePlate 永远不能为空,且长度必须在2到14个字符之间
seatCount 应至少为2.
从文档中我得到的印象是,在您的示例中,它将使用:
@Range(min= SEQ_MIN_VALUE, max= SEQ_MAX_VALUE)
Run Code Online (Sandbox Code Playgroud)
检查注释值是否位于(含)指定的最小值和最大值之间。支持的数据类型:
BigDecimal、BigInteger、CharSequence、byte、short、int、long 以及原始类型各自的包装器
| 归档时间: |
|
| 查看次数: |
91393 次 |
| 最近记录: |