How to Use @NotEmpty in Spring Boot for Validation
In Spring Boot, use the
@NotEmpty annotation on a field to ensure it is not null and not empty (for strings or collections). Combine it with @Valid in your controller to trigger validation automatically.Syntax
The @NotEmpty annotation is placed on a field to validate that it is neither null nor empty. It works on strings, collections, maps, and arrays.
Use it with @Valid on method parameters to activate validation in Spring Boot.
java
import jakarta.validation.constraints.NotEmpty; public class UserDTO { @NotEmpty(message = "Username must not be empty") private String username; // getters and setters }
Example
This example shows a Spring Boot REST controller that validates a user input object. If the username is empty, Spring Boot returns a validation error automatically.
java
import jakarta.validation.Valid; import jakarta.validation.constraints.NotEmpty; 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 UserDTO { @NotEmpty(message = "Username must not be empty") private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } } @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody UserDTO user) { return ResponseEntity.ok("User created: " + user.getUsername()); } }
Output
POST /users with body {"username":""} returns 400 Bad Request with validation error message "Username must not be empty"
Common Pitfalls
- Using
@NotEmptyon primitive types likeintwill not work because they cannot be empty or null. - For null checks only, use
@NotNull. For non-empty string checks, use@NotEmpty. For non-blank string checks, use@NotBlank. - For validation to work, you must add
@Validon the controller method parameter. - Make sure to include the
jakarta.validationdependency in your project.
java
/* Wrong usage: missing @Valid, validation won't trigger */ @PostMapping("/users") public ResponseEntity<String> createUser(@RequestBody UserDTO user) { return ResponseEntity.ok("User created: " + user.getUsername()); } /* Correct usage: add @Valid to trigger validation */ @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody UserDTO user) { return ResponseEntity.ok("User created: " + user.getUsername()); }
Quick Reference
@NotEmpty ensures a field is not null and not empty (for strings, collections, arrays).
Use with @Valid in Spring Boot controllers to activate validation.
Common alternatives:
@NotNull: only checks for null, allows empty strings.@NotBlank: checks for non-null and non-whitespace strings.
Key Takeaways
Use @NotEmpty on fields to ensure they are not null or empty in Spring Boot.
Add @Valid on controller method parameters to trigger validation automatically.
@NotEmpty works on strings, collections, maps, and arrays but not on primitives.
Include jakarta.validation dependency to use @NotEmpty and other validation annotations.
Choose @NotEmpty, @NotNull, or @NotBlank based on your validation needs.