Is there a way to implement @NotEmpty
hibernate validation without writing custom validation? javax.validation package does not contain this annotation. Only @NotNull
. But it does not validate for Non-null but empty values. So I would like to see an alternative for @NotEmpty
.
有没有办法在不编写自定义验证的情况下实现@NotEmpty hibernate验证? javax.validation包不包含此批注。只有@NotNull。但它不验证非空值而是空值。所以我想看看@NotEmpty的另一种选择。
Using @Pattern
? How?
使用@Pattern?怎么样?
3 个解决方案
#1
72
NotEmpty is just a combination of @NotNull
and @Size(min=1)
.
NotEmpty只是@NotNull和@Size(min = 1)的组合。
#2
6
Please be aware that @NotEmpty will return valid for a List<> containing a null element.
请注意,@ NotTmpty将对包含null元素的List <>返回有效。
Kind of bizarre in the case of a @QueryParam List<>
在@QueryParam List <>的情况下有点怪异
As say Affe, I did a custom annotation, itself annotated with @NotNull and @Size(min=1) with a custom validator that iterates the collection and positions a boolean flag only if the elements are not null.
正如Affe所说,我做了一个自定义注释,它本身用@NotNull和@Size(min = 1)注释了一个自定义验证器,它迭代集合并仅在元素不为空时才定位布尔标志。
#3
2
After Bean Validation 2.0 (JSR 380), There is a new annotation @NotEmpty:
在Bean Validation 2.0(JSR 380)之后,有一个新的注释@NotEmpty:
javax.validation.constraints.NotEmpty
See:
看到:
http://beanvalidation.org/2.0/spec/#builtinconstraints-notempty
http://beanvalidation.org/2.0/spec/#builtinconstraints-notempty
https://github.com/beanvalidation/beanvalidation-api/blob/master/src/main/java/javax/validation/constraints/NotEmpty.java
#1
72
NotEmpty is just a combination of @NotNull
and @Size(min=1)
.
NotEmpty只是@NotNull和@Size(min = 1)的组合。
#2
6
Please be aware that @NotEmpty will return valid for a List<> containing a null element.
请注意,@ NotTmpty将对包含null元素的List <>返回有效。
Kind of bizarre in the case of a @QueryParam List<>
在@QueryParam List <>的情况下有点怪异
As say Affe, I did a custom annotation, itself annotated with @NotNull and @Size(min=1) with a custom validator that iterates the collection and positions a boolean flag only if the elements are not null.
正如Affe所说,我做了一个自定义注释,它本身用@NotNull和@Size(min = 1)注释了一个自定义验证器,它迭代集合并仅在元素不为空时才定位布尔标志。
#3
2
After Bean Validation 2.0 (JSR 380), There is a new annotation @NotEmpty:
在Bean Validation 2.0(JSR 380)之后,有一个新的注释@NotEmpty:
javax.validation.constraints.NotEmpty
See:
看到:
http://beanvalidation.org/2.0/spec/#builtinconstraints-notempty
http://beanvalidation.org/2.0/spec/#builtinconstraints-notempty
https://github.com/beanvalidation/beanvalidation-api/blob/master/src/main/java/javax/validation/constraints/NotEmpty.java