0
点赞
收藏
分享

微信扫一扫

Bean Validation v2.0的常用标记

兽怪海北 2023-03-01 阅读 107


表格如下:

标记

说明

@Null

@NotNull

@AssertTrue

用于boolean或者Boolean,如果为null,认为是合法的,下同。当然我们可以和@NotNull

@Min

允许BigDecimal,BigInteger,byte,short,int,long,但不允许float和double。

@Max

@DecimalMin

除了@Min 允许的,还允许CharSequence(String)

@DecimalMax

@Negative

@NegativeOrZero

@Positive

@PositiveOrZero

@Size

CharSequence(String)的长度, Collection,Map, Array的size或长度,有两个参数:

min 缺省为 0

max 缺省为 Integer.MAX_VALUE

@Digits

处理数字,允许整形、CharSequence(String),float,Float,double,Double。

@Digits

(integer = 2/* 小数点前最多两位*/,fraction=2 /* 小数点后最多2位*/)

@Past

所注解的字段需要是一个过去的时间。

支持Date和Calendar,从2.0版本起,支持Java 8的Instant, LocalDate, LocalDateTime, LocalTime, MonthDay, OffsetDateTime, OffsetTime, Year, YearMonth, ZonedDateTime。

@PastOrPresent

所注解的字段需要是一个过去或现在的时间

@Future

所注解的字段需要是一个将来的时间

@FutureOrPresent

所注解的字段需要是一个现在或将来的时间

@Pattern

@NotNull

(message = “{validate.employee.email}”)

@Pattern

@NotEmpty

不允许为null和empty,针对CharSequence,Collection,Map,Array

@NotBlank

不允许null,只要有一个非空格的字符。针对CharSequence

@Email

beanvalidation.org官网文档(8.1-8.22共22个):
​​​https://beanvalidation.org/2.0/spec/#builtinconstraints-null​​


举报

相关推荐

0 条评论