@NotNull:// 可以用在所有类型上面,带有@NotNull注解的元素不能为null。对于集合类型的,没有size的约束。
@NotEmpty:// String, Collection, Map 和 Array 对象不能是 null 并且相关对象的 size 大于 0。
@NotBlank:// String去除了前后两段的空白字符串之后是否为null或者空的。
@NotNull:
看源码可知,@NotNull可以用在所有类型上面,带有@NotNull注解的元素不能为null。
/**
* The annotated element must not be {@code null}.
* Accepts any type.
*
* @author Emmanuel Bernard
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = { })
public @interface NotNull {
String message() default "{javax.validation.constraints.NotNull.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
/**
* Defines several {@link NotNull} annotations on the same element.
*
* @see javax.validation.constraints.NotNull
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@interface List {
NotNull[] value();
}
}
@NotEmpty:
看源码可知,带有@NotEmpty注解的String, Collection, Map或者Array不能为null或者为空。
@Size(min = 1)这个注解同时约束了集合的size的最小值为1,就是说空集合也会被判空。
/**
* Asserts that the annotated string, collection, map or array is not {@code null} or empty.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
@Documented
@Constraint(validatedBy = { })
@SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT)
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
@Size(min = 1)
public @interface NotEmpty {
String message() default "{org.hibernate.validator.constraints.NotEmpty.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
/**
* Defines several {@code @NotEmpty} annotations on the same element.
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
public @interface List {
NotEmpty[] value();
}
}
@NotBlank:
看源码注释可知,@NotBlank是用于判断String去除了前后两段的空白字符串之后是否为null或者空的。
/**
* Validate that the annotated string is not {@code null} or empty.
* The difference to {@code NotEmpty} is that trailing whitespaces are getting ignored.
*
* @author Hardy Ferentschik
*/
@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
public @interface NotBlank {
String message() default "{org.hibernate.validator.constraints.NotBlank.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
/**
* Defines several {@code @NotBlank} annotations on the same element.
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
public @interface List {
NotBlank[] value();
}
}
除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接
文章评论