0
0
Spring Bootframework~10 mins

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

Choose your learning style9 modes available
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.