0
0
Spring Bootframework~30 mins

@NotNull, @NotBlank, @NotEmpty in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Using @NotNull, @NotBlank, and @NotEmpty in Spring Boot Validation
📖 Scenario: You are building a simple Spring Boot application to register users. You want to make sure the user data is valid before saving it.
🎯 Goal: Learn how to use the validation annotations @NotNull, @NotBlank, and @NotEmpty on a user data class to enforce input rules.
📋 What You'll Learn
Create a User class with fields for name, email, and roles
Use @NotNull on the roles list to ensure it is not null
Use @NotBlank on the email field to ensure it is not empty or just spaces
Use @NotEmpty on the name field to ensure it is not empty
Add the necessary imports for validation annotations
💡 Why This Matters
🌍 Real World
Validating user input in web applications to prevent bad or missing data before saving to a database.
💼 Career
Understanding and applying validation annotations is essential for backend developers working with Spring Boot to build reliable APIs.
Progress0 / 4 steps
1
Create the User class with fields
Create a public class called User with three fields: String name, String email, and List<String> roles. Do not add any annotations yet.
Spring Boot
Need a hint?

Think of User as a box holding name, email, and roles information.

2
Add validation imports
Add import statements for javax.validation.constraints.NotNull, javax.validation.constraints.NotBlank, and javax.validation.constraints.NotEmpty at the top of the User class file.
Spring Boot
Need a hint?

These imports bring in the validation rules you will use on the fields.

3
Add validation annotations to fields
Add @NotEmpty above the name field, @NotBlank above the email field, and @NotNull above the roles field in the User class.
Spring Boot
Need a hint?

Each annotation enforces a different rule: @NotEmpty means the name cannot be empty, @NotBlank means email cannot be blank or spaces, and @NotNull means roles list cannot be null.

4
Complete the User class with public access and add getters
Make the User class public and add public getter methods for name, email, and roles fields.
Spring Boot
Need a hint?

Getters allow other parts of the program to read the field values safely.