0
0
Spring Bootframework~5 mins

@NotNull, @NotBlank, @NotEmpty in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the @NotNull annotation check in Spring Boot validation?
It checks that the annotated field is not null. However, it does not check if the field is empty if it is a String or collection.
Click to reveal answer
beginner
How is @NotBlank different from @NotNull?
@NotBlank checks that a String is not null and also not empty or only whitespace. It is stricter than @NotNull for Strings.
Click to reveal answer
beginner
What type of fields is @NotEmpty used for?
@NotEmpty is used for Strings, Collections, Maps, or arrays to ensure they are not null and not empty (length > 0).
Click to reveal answer
intermediate
Which annotation would you use to ensure a list is not null and has at least one element?
You would use @NotEmpty because it checks both for null and that the collection has at least one element.
Click to reveal answer
intermediate
Can @NotBlank be applied to a List or Collection?
No, @NotBlank is only for CharSequence (like String). For collections, use @NotEmpty to check for non-null and non-empty.
Click to reveal answer
Which annotation ensures a String is not null, not empty, and not just whitespace?
A@NotEmpty
B@NotNull
C@NotBlank
D@Valid
Which annotation can be used on a List to ensure it is not null and has elements?
A@NotBlank
B@NotNull
C@Size(min=1)
D@NotEmpty
What does @NotNull validate?
AField is not null
BField is not empty
CField is not blank
DField has minimum size
Which annotation would fail validation for a String with only spaces?
A@NotNull
B@NotBlank
CNone of these
D@NotEmpty
If you want to allow empty strings but not null, which annotation should you use?
A@NotNull
B@NotEmpty
CNo annotation needed
D@NotBlank
Explain the differences between @NotNull, @NotBlank, and @NotEmpty annotations in Spring Boot validation.
Think about what each annotation checks for and on which data types.
You got /3 concepts.
    Describe a real-life example where you would use @NotBlank instead of @NotNull.
    Consider a form field where blank spaces should not be accepted.
    You got /3 concepts.