Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java annotation for Null but neither Empty nor Blank

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 
like image 566
Yash Krishnan Avatar asked Jun 30 '15 07:06

Yash Krishnan


People also ask

What is @NotNull annotation in Java?

@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.

What is difference between @NotEmpty and @NotBlank?

@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.

What is @NotNull annotation in spring boot?

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.

What is @NotBlank in Java?

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.


2 Answers

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;     } } 
like image 61
vegemite4me Avatar answered Sep 19 '22 01:09

vegemite4me


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).

like image 23
Jules Avatar answered Sep 20 '22 01:09

Jules