Bird
Raised Fist0
Spring Bootframework~10 mins

@NotNull, @NotBlank, @NotEmpty in Spring Boot - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - @NotNull, @NotBlank, @NotEmpty
Start Validation
Check @NotNull
Yes / No
Fail: Value is null
Yes
Check @NotEmpty
Yes / No
Fail: Collection/String is empty
Yes
Check @NotBlank
Yes / No
Fail: String is blank
Yes
Validation Passes
Validation checks run in order: first @NotNull ensures value is not null, then @NotEmpty checks collections or strings are not empty, finally @NotBlank ensures strings have non-whitespace content.
Execution Sample
Spring Boot
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

public class User {
  @NotNull
  private String id;

  @NotEmpty
  private List<String> roles;

  @NotBlank
  private String name;
}
This code defines a User class with fields validated by @NotNull, @NotEmpty, and @NotBlank annotations.
Execution Table
StepFieldAnnotationValueValidation ResultAction
1id@NotNullnullFailReject: id cannot be null
2id@NotNull"123"PassContinue
3roles@NotEmpty[]FailReject: roles cannot be empty
4roles@NotEmpty["admin"]PassContinue
5name@NotBlank" "FailReject: name cannot be blank
6name@NotBlank"Alice"PassValidation passes
💡 Validation stops at first failure or passes if all checks succeed
Variable Tracker
FieldInitial ValueAfter Validation
idnullValidation failed if null
id"123"Validation passed
roles[]Validation failed if empty
roles["admin"]Validation passed
name" "Validation failed if blank
name"Alice"Validation passed
Key Moments - 3 Insights
Why does @NotNull fail but @NotEmpty might not?
@NotNull fails if the value is null (no object), while @NotEmpty fails if the value exists but is empty (like an empty list or string). See execution_table rows 1 and 3.
Can @NotBlank be used on a collection?
@NotBlank only works on strings to check for non-whitespace content. Collections need @NotEmpty. See execution_table row 5 for @NotBlank on a string.
What happens if a string is " " (spaces) with @NotEmpty?
@NotEmpty passes because the string is not empty, but @NotBlank fails because it only has whitespace. See execution_table rows 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the validation result for 'roles' when the value is []?
APass
BFail
CError
DSkipped
💡 Hint
Check row 3 in the execution_table where roles is []
At which step does the validation pass for the 'name' field?
AStep 4
BStep 5
CStep 6
DStep 3
💡 Hint
Look at the execution_table rows for 'name' field validation
If the 'id' field is set to "123", what happens at step 2?
AValidation passes and continues
BValidation fails because id is empty
CValidation fails because id is null
DValidation is skipped
💡 Hint
See execution_table row 2 for id with value "123"
Concept Snapshot
@NotNull: Fails if value is null
@NotEmpty: Fails if collection or string is empty
@NotBlank: Fails if string is blank (only whitespace)
Use @NotNull for any object
Use @NotEmpty for collections or strings
Use @NotBlank only for strings with content
Full Transcript
This visual execution shows how Spring Boot validation annotations @NotNull, @NotEmpty, and @NotBlank work step-by-step. First, @NotNull checks if a value is null and fails if so. Then, @NotEmpty checks if collections or strings are empty and fails if empty. Finally, @NotBlank checks if strings contain only whitespace and fails if blank. The execution table traces example values through these checks, showing when validation passes or fails. Key moments clarify common confusions, like the difference between null and empty, and when to use each annotation. The quiz tests understanding by referencing the execution steps and variable states.

Practice

(1/5)
1. Which annotation ensures a string field in a Spring Boot application is not null, but allows empty strings?
easy
A. @NotEmpty
B. @NotBlank
C. @NotNull
D. @Valid

Solution

  1. Step 1: Understand @NotNull behavior

    @NotNull only prevents the value from being null but allows empty strings or collections.
  2. Step 2: Compare with other annotations

    @NotEmpty and @NotBlank prevent empty values, so they are stricter than @NotNull.
  3. Final Answer:

    @NotNull -> Option C
  4. Quick Check:

    @NotNull allows empty but not null [OK]
Hint: Remember: @NotNull forbids null, allows empty [OK]
Common Mistakes:
  • Confusing @NotNull with @NotEmpty or @NotBlank
  • Thinking @NotNull forbids empty strings
  • Assuming @Valid checks null automatically
2. Which of the following is the correct way to annotate a string field that must not be null, empty, or only spaces in Spring Boot?
easy
A. @NotBlank private String name;
B. @NotEmpty private String name;
C. @NotNull @NotEmpty private String name;
D. @NotNull private String name;

Solution

  1. Step 1: Identify annotation that forbids null, empty, and spaces

    @NotBlank ensures the string is not null, not empty, and not just whitespace.
  2. Step 2: Check other options

    @NotNull allows empty or spaces; @NotEmpty forbids empty but allows spaces; only @NotBlank covers all three.
  3. Final Answer:

    @NotBlank private String name; -> Option A
  4. Quick Check:

    @NotBlank forbids null, empty, spaces [OK]
Hint: Use @NotBlank for strings with no spaces or empties [OK]
Common Mistakes:
  • Using @NotEmpty but ignoring spaces
  • Combining @NotNull and @NotEmpty unnecessarily
  • Assuming @NotNull forbids empty strings
3. Given the code snippet:
@NotEmpty
private List<String> tags;

What happens if tags is set to an empty list during validation?
medium
A. Validation passes because list is not null
B. Validation fails because list is empty
C. Validation fails only if list is null
D. Validation passes regardless of list content

Solution

  1. Step 1: Understand @NotEmpty on collections

    @NotEmpty forbids null or empty collections, so empty list fails validation.
  2. Step 2: Compare with other annotations

    @NotNull allows empty list; @NotEmpty forbids empty; @NotBlank is for strings only.
  3. Final Answer:

    Validation fails because list is empty -> Option B
  4. Quick Check:

    @NotEmpty forbids empty collections [OK]
Hint: @NotEmpty forbids empty lists or strings [OK]
Common Mistakes:
  • Thinking empty list passes @NotEmpty
  • Confusing @NotEmpty with @NotNull
  • Applying @NotBlank to collections
4. Consider this code snippet:
@NotBlank
private String title;

Which of the following values for title will cause validation to fail with @NotBlank but pass with @NotEmpty?
medium
A. " " (string with spaces only)
B. "Hello"
C. "" (empty string)
D. null

Solution

  1. Step 1: Understand @NotBlank validation rules

    @NotBlank forbids null, empty, and strings with only whitespace.
  2. Step 2: Analyze each value

    null and "" fail both @NotBlank and @NotEmpty; "Hello" passes both; " " passes @NotEmpty (length > 0) but fails @NotBlank (trimmed length = 0).
  3. Final Answer:

    " " (string with spaces only) -> Option A
  4. Quick Check:

    @NotBlank forbids spaces-only strings [OK]
Hint: @NotBlank forbids spaces-only strings [OK]
Common Mistakes:
  • Assuming empty string passes @NotBlank
  • Confusing @NotBlank with @NotEmpty
  • Ignoring spaces-only strings as invalid
5. You want to validate a user input field description that must not be null, empty, or only spaces, but also must allow strings like "0" or "false". Which annotation should you use in Spring Boot?
hard
A. @Size(min=1)
B. @NotEmpty
C. @NotNull
D. @NotBlank

Solution

  1. Step 1: Understand requirements for description

    Must not be null, empty, or spaces-only, but allow "0" or "false" strings.
  2. Step 2: Match annotation behavior

    @NotBlank forbids null, empty, and spaces-only strings but allows "0" and "false" as they are non-blank strings.
  3. Step 3: Check alternatives

    @NotNull allows empty; @NotEmpty forbids empty but allows spaces; @Size(min=1) forbids empty but not spaces-only.
  4. Final Answer:

    @NotBlank -> Option D
  5. Quick Check:

    @NotBlank fits all conditions [OK]
Hint: Use @NotBlank to forbid null, empty, and spaces-only [OK]
Common Mistakes:
  • Using @NotEmpty and missing spaces-only strings
  • Assuming @Size(min=1) forbids spaces-only
  • Confusing @NotNull with stricter annotations