Spring Boot Validation with @NotNull Annotation

Hi everyone 👋 Continuing the Spring Boot Validation Series Part 27 👇 📌 Validation Annotation – @NotNull The @NotNull annotation is used to ensure that a field cannot be null 👇 🔹 Why do we use @NotNull? When we receive data from users or APIs, we want to make sure some fields are always present. 👉 @NotNull helps us enforce this rule. 🔹 Simple Example - public class User { @NotNull private String name; private String email; } @PostMapping("/users") public String createUser(@Valid @RequestBody User user) { return "User created"; } 👉 If name is null → validation fails ❌ 🔹 Important Point 👉 @NotNull checks only for null value It does NOT check: Empty string ❌ Blank value ❌ 🔹 Difference @NotNull - Value should not be null @NotEmptyNot - null + not empty\ @NotBlankNot - null + not empty + no only spaces. 🔹 Example for clarity @NotNull // allows "" (empty) @NotEmpty // does NOT allow "" @NotBlank // does NOT allow "" or " " 🔹 In simple words @NotNull makes sure that the value is not null, but it can still be empty. 👉 🧠 Quick Understanding - Checks only for null - Does not validate empty or blank - Used with @Valid - Important for required fields #SpringBoot #Java #Validation #NotNull #BackendDevelopment #LearningInPublic

To view or add a comment, sign in

Explore content categories