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
.
Using @Pattern
? How?
@NotNull : The CharSequence, Collection, Map or Array object is not null, but can be empty. @NotEmpty : The CharSequence, Collection, Map or Array object is not null and size > 0. @NotBlank : The string is not null and the trimmed length is greater than zero.
Annotation Type NotBlankDeprecated. Validate that the annotated string is not null or empty. The difference to NotEmpty is that trailing whitespaces are getting ignored.
@NotNull The @NotNull annotation is, actually, an explicit contract declaring that: A method should not return null. Variables (fields, local variables, and parameters) cannot hold a null value.
Simply put, Javax validation works with two main group of annotations. Annotations to ensure that validation happens like @Valid or @Validated. Annotations to denote the kind of constraints to be enforced on a value like @NotNull, @Pattern.
NotEmpty is just a combination of @NotNull
and @Size(min=1)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With