Reputation: 2119
The structure that I have is something like below:
Class A{
String str;
int i;
List<B> bs;
C c;
@NotNull
List<D> ds;
}
Class B{
@NotNull
List<E> es;
}
Class C{
List<String> s;
}
Class E{
@NotNull
List<String> s;
}
For the list variables that are annotated with @NotNull I need to throw validation error if any of them variables has one or more null objects. While for the other list variables I just need to remove the nulls; What would be the best way to achieve this?
Upvotes: 1
Views: 1830
Reputation: 2119
This is the final code that I wrote, just a few tweaks to the code that Hadi posted. I hope it helps: Annotation:
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ListValidator.class)
@Documented
public @interface ValidList {
String message() default "Null values are not allowed in array fields.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Validator Class:
import java.util.List;
import java.util.Objects;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class ListValidator implements ConstraintValidator<ValidList, List<? extends Object>> {
@Override
public boolean isValid(List<? extends Object> list, ConstraintValidatorContext context) {
return list.stream().noneMatch(Objects::isNull);
}
@Override
public void initialize(ValidList constraintAnnotation) {}
}
Upvotes: 0
Reputation: 6956
If you are using validation 2.0+ you can put annotation inside: List<@NotNull String> s;
Upvotes: 1
Reputation: 17299
You should define custom annotation for validating.
so define custom annotation like bellow.
@Target({ElementType.FIELD, ElementType.PARAMETER,ElementType.ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = ValidateListValidator.class)
@Documented
public @interface ValidateList {
}
and implement ValidateListValidator
like this:
public class ValidateListValidator implements ConstraintValidator<ValidateList, List<Object>> {
private ValidateList validateList;
@Override
public void initialize(ValidateList validateList) {
this.validateList = validateList;
}
@Override
public boolean isValid( List<Object> list, ConstraintValidatorContext constraintValidatorContext) {
return list.stream().noneMatch(Objects::isNull);
}
}
and for test it
@Test
public void test() {
boolean valid = validator.isValid(Arrays.asList("test","this",null),context);
assertThat(valid, is(false));
}
Upvotes: 1