Performance: @Size for length constraints
@Size affects form validation speed and user experience by checking input length before processing.
Jump into concepts and practice - no test required
import jakarta.validation.constraints.Size; public class User { @Size(min = 3, max = 20) private String username; // getters and setters }
public class User { private String username; public void setUsername(String username) { if(username == null || username.length() < 3 || username.length() > 20) { throw new IllegalArgumentException("Invalid length"); } this.username = username; } }
| Pattern | Validation Timing | Code Complexity | Server Load | Verdict |
|---|---|---|---|---|
| Manual length check in setter | Late (during setter call) | High (repeated code) | Higher (extra processing) | [X] Bad |
| @Size annotation on field | Early (before logic) | Low (declarative) | Lower (early rejection) | [OK] Good |
@Size annotation do in Spring Boot validation?min and max to set length limits on text or collections.@Size to require a string between 5 and 10 characters?min and max for length limits.@Size(min=3, max=6) private String code;
@Size(min=2, max=5) private int number;
@Size in your Spring Boot model?