Hibernate Validator 校验注解

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检查带注释的元素为trueBoolean,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考虑给定标志,检查带注释的字符串是否与正则表达式匹配matchCharSequence
@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;
}
posted @ 2020-07-06 18:27  预立科技  阅读(64)  评论(0编辑  收藏  举报