How to Use @NotNull in Spring Boot for Validation
In Spring Boot, use the
@NotNull annotation from jakarta.validation.constraints on fields or method parameters to ensure they are not null during validation. Combine it with @Valid in controller methods to trigger validation automatically.Syntax
The @NotNull annotation is placed on a field, method, or parameter to indicate it must not be null. It is part of Jakarta Bean Validation and requires a validation framework like Hibernate Validator.
@NotNull: Ensures the annotated element is not null.- Used with
@Validin Spring controllers to trigger validation. - Works on fields, method parameters, and return values.
java
import jakarta.validation.constraints.NotNull; public class User { @NotNull(message = "Name must not be null") private String name; // getters and setters }
Example
This example shows a Spring Boot REST controller that validates a request body with a @NotNull field. If the field is null, Spring returns a 400 Bad Request with the validation message.
java
import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { public static class User { @NotNull(message = "Name must not be null") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody User user) { return ResponseEntity.ok("User created: " + user.getName()); } }
Output
POST /users with JSON {"name":null} returns 400 Bad Request with message: "Name must not be null"
POST /users with JSON {"name":"Alice"} returns 200 OK with body: "User created: Alice"
Common Pitfalls
- Forgetting to add
@Validon the controller method parameter, so validation does not run. - Using
@NotNullbut sending empty strings or blank values;@NotNullonly checks for null, not emptiness. - Not including a validation provider like Hibernate Validator in your project dependencies.
- Confusing
@NotNullwith@NotEmptyor@NotBlankwhich check for empty or blank strings.
java
/* Wrong: Missing @Valid, validation won't trigger */ @PostMapping("/users") public ResponseEntity<String> createUser(@RequestBody User user) { return ResponseEntity.ok("User created: " + user.getName()); } /* Right: Add @Valid to trigger validation */ @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody User user) { return ResponseEntity.ok("User created: " + user.getName()); }
Quick Reference
- @NotNull: Ensures value is not null.
- @Valid: Triggers validation on method parameters or fields.
- Use with Spring Boot's
spring-boot-starter-validationdependency. - Validation errors return HTTP 400 with messages by default.
Key Takeaways
Use @NotNull on fields or parameters to enforce non-null values in Spring Boot.
Always add @Valid on controller method parameters to activate validation.
Include spring-boot-starter-validation dependency for validation support.
@NotNull checks only for null, not empty or blank strings.
Validation failures automatically return HTTP 400 with error details.