How to Use @Size Annotation in Spring Boot for Validation
In Spring Boot, use the
@Size annotation on fields to validate that their length or size is within specified limits. It works on strings, collections, arrays, and maps, and you specify minimum and maximum values like @Size(min=2, max=10). Combine it with @Valid in your controller to trigger validation automatically.Syntax
The @Size annotation is used to specify minimum and maximum size constraints on a field. It can be applied to strings, collections, arrays, and maps.
- min: The minimum allowed size (default is 0).
- max: The maximum allowed size (default is
Integer.MAX_VALUE). - message: Custom error message when validation fails.
java
@Size(min = 2, max = 10, message = "Size must be between 2 and 10") private String name;
Example
This example shows a Spring Boot REST controller validating a user input object with a @Size constraint on the username field. If the username length is outside 3 to 15 characters, validation fails and returns an error.
java
import jakarta.validation.constraints.Size; import jakarta.validation.Valid; 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 { @Size(min = 3, max = 15, message = "Username must be between 3 and 15 characters") private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } } @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody User user) { return ResponseEntity.ok("User " + user.getUsername() + " created successfully"); } }
Output
POST /users with JSON {"username":"Jo"} returns 400 Bad Request with message "Username must be between 3 and 15 characters"
POST /users with JSON {"username":"JohnDoe"} returns 200 OK with message "User JohnDoe created successfully"
Common Pitfalls
- For validation to work, you must add
@Validon the controller method parameter. @Sizeonly checks length or size, not null values. Use@NotNullif null is not allowed.- Applying
@Sizeon numeric types likeintwill not work; it is for strings and collections. - Custom messages should be clear and user-friendly.
java
/* Wrong: Missing @Valid, validation will not trigger */ @PostMapping("/users") public ResponseEntity<String> createUser(@RequestBody User user) { return ResponseEntity.ok("User created"); } /* Right: Add @Valid to trigger validation */ @PostMapping("/users") public ResponseEntity<String> createUser(@Valid @RequestBody User user) { return ResponseEntity.ok("User created"); }
Quick Reference
| Property | Description | Default |
|---|---|---|
| min | Minimum size allowed | 0 |
| max | Maximum size allowed | Integer.MAX_VALUE |
| message | Custom error message | "size must be between {min} and {max}" |
Key Takeaways
Use @Size to enforce length or size limits on strings, collections, arrays, or maps.
Always add @Valid on controller method parameters to activate validation.
Combine @Size with @NotNull if null values should be disallowed.
Customize error messages with the message attribute for better user feedback.
@Size does not work on numeric types; use other annotations for numbers.