我们在做web项目的时候经常会遇到前端传参的情况,这会需要对参数做校验,比如参数不能为空,集合不能为空等校验,javax.validation.constraints为我们提供了很多注解来帮忙我们做参数校验。
1、 @NotNull
The annotated element must not be {@code null}.
校验参数一定不能为null,但是可以为" "。
2、 @NotEmpty
The annotated element must not be {@code null} nor empty. Supported types are:
1、{@code CharSequence} (length of character sequence is evaluated);
2、{@code Collection} (collection size is evaluated);
3、{@code Map} (map size is evaluated);
4、Array (array length is evaluated);
校验集合类参数(如String类、Collection、Map、数据Array)不能为null或empty。其中String的length、Collection和Map的size不能为0。
3、@NotBlank
The annotated element must not be {@code null} and must contain at least one non-whitespace character. Accepts {@code CharSequence}.
校验String字符串不能为null,且不能是空字符串(" "),即调用trim()之后字符串的长度不能为0。
4、@PositiveOrZero
The annotated element must be a positive number or 0.
校验参数必须是正整数或0。