Is there are any java annotation(s) that can validate like the example below?
String test; test = null; //valid test = ""; //invalid test = " "; //invalid test = "Some values"; //valid
@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.
@NotEmpty: a constrained CharSequence, Collection, Map, or Array is valid as long as it's not null, and its size/length is greater than zero. @NotBlank: a constrained String is valid as long as it's not null, and the trimmed length is greater than zero.
Overview. NullPointerExceptions are a common problem. One way we can protect our code is to add annotations such as @NotNull to our method parameters. By using @NotNull, we indicate that we must never call our method with a null if we want to avoid an exception. However, by itself, that's not enough.
notBlank() is a static method of the Validate class that is used to check that the specified argument character sequence is not null , a length of zero, empty, or whitespace. Otherwise, the method throws an exception with the specified message.
You need to create a custom annotation: @NullOrNotBlank
First create the custom annotation: NullOrNotBlank.java
@Target( {ElementType.FIELD}) @Retention(RUNTIME) @Documented @Constraint(validatedBy = NullOrNotBlankValidator.class) public @interface NullOrNotBlank { String message() default "{javax.validation.constraints.NullOrNotBlank.message}"; Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default {}; }
Then the actual validator: NullOrNotBlankValidator.java
public class NullOrNotBlankValidator implements ConstraintValidator<NullOrNotBlank, String> { public void initialize(NullOrNotBlank parameters) { // Nothing to do here } public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { return value == null || value.trim().length() > 0; } }
There isn't such an annotation in either javax.validation or Hibernate Validator. There was a request to add one to Hibernate Validator but it was closed as "won't fix" due to the possibility of writing your own relatively easily. The suggest solution was to either use your own annotation type defined like this:
@ConstraintComposition(OR) @Null @NotBlank @ReportAsSingleViolation @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @Constraint(validatedBy = { }) public @interface NullOrNotBlank { String message() default "{org.hibernate.validator.constraints.NullOrNotBlank.message}"; Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default { }; }
or to use the @Pattern
annotation with a regular expression that requires a non-whitespace character to be present (as the Pattern annotation accepts nulls and does not match them against the pattern).
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