0
0
Spring Bootframework~5 mins

@Size for length constraints in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Size annotation in Spring Boot?
The @Size annotation is used to set minimum and maximum length constraints on a string, collection, map, or array to ensure the data meets length requirements before processing.
Click to reveal answer
beginner
How do you specify the minimum and maximum length using @Size?
You use the attributes min and max inside @Size, for example: @Size(min = 3, max = 10) means the value must be at least 3 and at most 10 characters long.
Click to reveal answer
intermediate
Can @Size be used on collections like List or arrays?
Yes, @Size works on collections, arrays, and maps to restrict the number of elements they contain, not just strings.
Click to reveal answer
intermediate
What happens if a value violates the @Size constraints in Spring Boot?
Spring Boot's validation framework will detect the violation and typically return an error response indicating the size constraint was not met, preventing further processing.
Click to reveal answer
beginner
Write a simple example of @Size used on a username field in a Spring Boot model.
Example: <br> @Size(min = 5, max = 15, message = "Username must be between 5 and 15 characters")<br>private String username;
Click to reveal answer
What does @Size(min = 2, max = 8) enforce on a string?
AThe string length must be between 2 and 8 characters inclusive
BThe string length must be exactly 2 or 8 characters
CThe string length must be less than 2 or greater than 8
DThe string length must be at least 8 characters
Can @Size be applied to a List in Spring Boot?
AYes, it limits the number of elements in the List
BNo, it only works on strings
CYes, but only for arrays, not Lists
DNo, it only works on numeric values
What attribute of @Size sets the maximum allowed length?
Alimit
Bmaximum
Cmax
Dlength
If a field annotated with @Size(min=3) has a value of length 2, what happens?
AThe value is automatically padded to length 3
BValidation fails and an error is returned
CValidation passes silently
DThe application crashes
Which package provides the @Size annotation in Spring Boot?
Aorg.springframework.validation
Bjava.util.validation
Corg.springframework.boot.validation
Djavax.validation.constraints
Explain how @Size helps in validating user input in Spring Boot applications.
Think about how length limits protect data quality.
You got /4 concepts.
    Describe a scenario where using @Size on a List would be useful.
    Consider forms where users select multiple options.
    You got /4 concepts.