Hibernate Validator 校验注解:

一、@NotBlank、@NotEmpty、@NotNull

@NotBlank 字符序列不为null,并且长度大于0(不包括:空字符串或都是空格的字符串)
@NotEmpty 字符序列不为null,但可以为空字符串或都是空格的字符串
@NotNull 字符序列不为null



二、约束说明

约束

用法说明

应用数据类型

@NotBlank

检查带注释的字符序列不为null,

并且长度大于0

CharSequence

@NotEmpty

检查带注释的元素是否不为null或为空

CharSequence,Collection,Map和数组

@NotNull

检查注释的值不是null

任何类型

@Range(min=,max=)

检查带注释的值是否介于(包括)指定的最小值和最大值之间

BigDecimal,BigInteger,CharSequence,byte,short,int,long

@Max(value=)

检查带注释的值是否小于或等于指定的最大值

BigDecimal,BigInteger,byte,short,int,long

@Min(value=)

检查带注释的值是否大于或等于指定的最小值

BigDecimal,BigInteger,byte,short,int,long

@Email

检查指定的字符序列是否为有效的电子邮件地址。可选参数regexp,flags允许您指定电子邮件必须匹配的其他正则表式

CharSequence

@Size(min=,max=)

检查带注释的元素的大小是否介于min和之间max(包括)

CharSequence,Collection,Map和数组

@URL(protocol=,

host=,port=,

regexp=,flags=)

可选参数protocol,host或port指定时,相应的URL片段必须在指定的值相匹配。regexp并flags允许指定URL必须匹配的其他正则表达式

CharSequence

@AssertFalse

检查带注释的日期是否是将来的日期

Boolean,boolean

@AssertTrue

检查带注释的元素为true

Boolean,boolean

@DecimalMax(value=,

inclusive=)

inclusive=false时,检查带注释的值是否小于指定的最大值。inclusive=true时,该值是否小于或等于指定的最大值。

BigDecimal,BigInteger,CharSequence,byte,short,int,long

@DecimalMin(value=,

inclusive=)

inclusive=false时,检查带注释的值是否大于指定的最小值。inclusive=true时,该值是否大于或等于指定的最小值。

BigDecimal,BigInteger,CharSequence,byte,short,int,long

@Digits(integer=,fraction=)

检查带注释的值是否是最多由integer数字和fraction小数位组成的数字

BigDecimal,BigInteger,CharSequence,byte,short,int,long

@Future

检查带注释的日期是否是将来的日期

Date,Calendar,Instant,LocalDate,LocalDateTime,LocalTime,MonthDay,OffsetDateTime,OffsetTime,Year,YearMonth,ZonedDateTime,HijrahDate,JapaneseDate,MinguoDate,ThaiBuddhistDate

@FutureOrPresent

查带注释的日期是现在还是将来

Date,Calendar,Instant,LocalDate,LocalDateTime,LocalTime,MonthDay,OffsetDateTime,OffsetTime,Year,YearMonth,ZonedDateTime,HijrahDate,JapaneseDate,MinguoDate,ThaiBuddhistDate

@Negative

检查元素是否严格为负。零值被视为无效。

BigDecimal,BigInteger,byte,short,int,long

@NegativeOrZero

检查元素是负数或零。

BigDecimal,BigInteger,byte,short,int,long

@Null

检查注释的值是null

任何类型

@Past

检查带注释的日期是否是过去的日期

Date,Calendar,Instant,LocalDate,LocalDateTime,LocalTime,MonthDay,OffsetDateTime,OffsetTime,Year,YearMonth,ZonedDateTime,HijrahDate,JapaneseDate,MinguoDate,ThaiBuddhistDate

@PastOrPresent

检查带注释的日期是过去还是现在

Date,Calendar,Instant,LocalDate,LocalDateTime,LocalTime,MonthDay,OffsetDateTime,OffsetTime,Year,YearMonth,ZonedDateTime,HijrahDate,JapaneseDate,MinguoDate,ThaiBuddhistDate

@Pattern(regex=,flags=)

regex考虑给定标志,检查带注释的字符串是否与正则表达式匹配match

CharSequence

@Positive

检查元素是否严格为正。零值被视为无效。

BigDecimal,BigInteger,byte,short,int,long

@PositiveOrZero

检查元素是正数还是零。

BigDecimal,BigInteger,byte,short,int,long


二、定义不满足约束的消息

1.上面约束都都默认约束违规消息,当然我们也可以自定义

@NotNull(message = "name不为空")
private String name;

@Min(value = 2,message = "count最小值应为{value}")
private int count;



2.单个参数约束

public void test2(
        @NotNull @Future Date startDate,
        @Min(1) int count) {
}



3.类参数约束

public class User {

    @NotNull
    private String name;

    @Min(18)
    private int  age;

    //geter...  seter...
}

public class Test {
    public boolean Test1(@Valid User user) {
        //...
        return false;
    }
}

增加@Valid注解后,表示需要对其中的参数进行校验。



**4.返回值约束**

@NotNull
@Size(min = 1)
public List<@NotNull User> getUsers() {
    //...
    return null;
}