Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of @NotNull.List

When I looked among the standard constraints in Bean Validation API (JSR-303), I found the NotNull.List annotation. Its description is:

Defines several @NotNull annotations on the same element

This is valid syntax:

@NotNull.List({@NotNull, @NotNull})
private Object myObject;

But it makes no sense. Either the object is null or it is not. When would you use this annotation?

There are several other similar annotations like AssertFalse.List and AssertTrue.List.

like image 514
holmis83 Avatar asked Feb 19 '15 15:02

holmis83


People also ask

What is the use of @NotNull?

The @NotNull Annotation is, actually, an explicit contract declaring the following: A method should not return null. A variable (like fields, local variables, and parameters) cannot should not hold null value.

What does NotNull annotation do?

@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 does javax validation constraints NotNull do?

@NotNull validates that the annotated property value is not null. @AssertTrue validates that the annotated property value is true.

What is the difference between @NotBlank and @NotNull?

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

Which exception is thrown by @NotNull?

Hi, when using the @NotNull annotation, IntelliJ will generate bytecode to throw an IllegalArgumentException when a null is passed/returned.

What is the use of @nullable annotation?

The @NonNull/@Nullable annotation can put in front of functions as well to indicate the return value nullability. return null; // warn: 'null' is returned by the method ... Easy, right? Keep your Billion Dollar in your pocket and enjoy using this @Nullable and @NonNull.


1 Answers

You can have multiple @NotNull annotations that are mutually exclusive based on the group attribute.

@NotNull.List({@NotNull(groups=Foo.class,message="Some message!"), 
               @NotNull(groups=bar.class, message="Some other message!"})
private Object myObject;

I do agree it's a little silly for this example since only the payload and message can be affected, but it's probably there to remain consistent with the other annotations.

See here for more details.

like image 67
dfb Avatar answered Oct 03 '22 09:10

dfb