These annotations help check if data is missing or empty before saving or using it. They keep your app safe and correct.
@NotNull, @NotBlank, @NotEmpty in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
@NotNull @NotEmpty @NotBlank
@NotNull means the value cannot be null but can be empty if it is a string or collection.
@NotEmpty means the value cannot be null or empty (like an empty string or empty list).
@NotBlank means the value cannot be null, empty, or only spaces (only for strings).
@NotNull private Integer age;
@NotEmpty private List<String> tags;
@NotBlank private String name;
This class uses the three annotations to make sure the user has an ID, roles list, and username filled properly. The main method shows creating a user and printing the values.
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; public class User { @NotNull private Integer id; @NotEmpty private List<String> roles; @NotBlank private String username; // Constructor public User(Integer id, List<String> roles, String username) { this.id = id; this.roles = roles; this.username = username; } // Getters public Integer getId() { return id; } public List<String> getRoles() { return roles; } public String getUsername() { return username; } public static void main(String[] args) { // Example usage User user = new User(1, List.of("admin", "user"), "john_doe"); System.out.println("User ID: " + user.getId()); System.out.println("Roles: " + user.getRoles()); System.out.println("Username: " + user.getUsername()); } }
These annotations work with Spring Boot's validation system and need a validator to check them at runtime.
@NotBlank only works on strings, so use @NotEmpty for collections or arrays.
Use these annotations on fields or method parameters to automatically validate input data.
@NotNull means value cannot be null but can be empty.
@NotEmpty means value cannot be null or empty (strings, collections).
@NotBlank means value cannot be null, empty, or spaces (only strings).
Practice
Solution
Step 1: Understand @NotNull behavior
@NotNull only prevents the value from being null but allows empty strings or collections.Step 2: Compare with other annotations
@NotEmpty and @NotBlank prevent empty values, so they are stricter than @NotNull.Final Answer:
@NotNull -> Option CQuick Check:
@NotNull allows empty but not null [OK]
- Confusing @NotNull with @NotEmpty or @NotBlank
- Thinking @NotNull forbids empty strings
- Assuming @Valid checks null automatically
Solution
Step 1: Identify annotation that forbids null, empty, and spaces
@NotBlank ensures the string is not null, not empty, and not just whitespace.Step 2: Check other options
@NotNull allows empty or spaces; @NotEmpty forbids empty but allows spaces; only @NotBlank covers all three.Final Answer:
@NotBlank private String name; -> Option AQuick Check:
@NotBlank forbids null, empty, spaces [OK]
- Using @NotEmpty but ignoring spaces
- Combining @NotNull and @NotEmpty unnecessarily
- Assuming @NotNull forbids empty strings
@NotEmpty private List<String> tags;
What happens if
tags is set to an empty list during validation?Solution
Step 1: Understand @NotEmpty on collections
@NotEmpty forbids null or empty collections, so empty list fails validation.Step 2: Compare with other annotations
@NotNull allows empty list; @NotEmpty forbids empty; @NotBlank is for strings only.Final Answer:
Validation fails because list is empty -> Option BQuick Check:
@NotEmpty forbids empty collections [OK]
- Thinking empty list passes @NotEmpty
- Confusing @NotEmpty with @NotNull
- Applying @NotBlank to collections
@NotBlank private String title;
Which of the following values for
title will cause validation to fail with @NotBlank but pass with @NotEmpty?Solution
Step 1: Understand @NotBlank validation rules
@NotBlank forbids null, empty, and strings with only whitespace.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).Final Answer:
" " (string with spaces only) -> Option AQuick Check:
@NotBlank forbids spaces-only strings [OK]
- Assuming empty string passes @NotBlank
- Confusing @NotBlank with @NotEmpty
- Ignoring spaces-only strings as invalid
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?Solution
Step 1: Understand requirements for description
Must not be null, empty, or spaces-only, but allow "0" or "false" strings.Step 2: Match annotation behavior
@NotBlank forbids null, empty, and spaces-only strings but allows "0" and "false" as they are non-blank strings.Step 3: Check alternatives
@NotNull allows empty; @NotEmpty forbids empty but allows spaces; @Size(min=1) forbids empty but not spaces-only.Final Answer:
@NotBlank -> Option DQuick Check:
@NotBlank fits all conditions [OK]
- Using @NotEmpty and missing spaces-only strings
- Assuming @Size(min=1) forbids spaces-only
- Confusing @NotNull with stricter annotations
