java / beginner
Snippet
Basic Field Validation
Spring supports Bean Validation to enforce rules on your data models. Annotations like @NotNull and @Size ensure that fields meet specific criteria before processing.
snippet.java
1
2
3
4
5
public class UserProfile {@NotNull@Size(min = 3, max = 20)private String username;}
spring
Breakdown
1
@NotNull
Ensures the username field is never null.
2
@Size(min = 3, max = 20)
Constraints the username to be between 3 and 20 characters long.