@Min and @Max help make sure numbers stay within limits you set. This stops wrong or unexpected numbers from causing problems.
@Min, @Max for numeric constraints in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
@Min(value) @Max(value) // Example: @Min(1) @Max(10) private int quantity;
Use these annotations on numeric fields like int, long, or their wrapper classes.
The value inside @Min or @Max is the limit number you want to enforce.
@Min(18) private int age;
@Max(100) private int age;
@Min(1) @Max(5) private int rating;
This program creates a Product class with a quantity field limited between 1 and 10 using @Min and @Max.
It then validates three products: one valid (5), one too low (0), and one too high (15).
The program prints validation results for each product.
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Max; import jakarta.validation.Validation; import jakarta.validation.Validator; import jakarta.validation.ValidatorFactory; import jakarta.validation.ConstraintViolation; import java.util.Set; public class Product { @Min(1) @Max(10) private int quantity; public Product(int quantity) { this.quantity = quantity; } public int getQuantity() { return quantity; } public static void main(String[] args) { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); Product p1 = new Product(5); Product p2 = new Product(0); Product p3 = new Product(15); validateProduct(p1, validator); validateProduct(p2, validator); validateProduct(p3, validator); } private static void validateProduct(Product product, Validator validator) { Set<ConstraintViolation<Product>> violations = validator.validate(product); if (violations.isEmpty()) { System.out.println("Product quantity " + product.getQuantity() + " is valid."); } else { for (ConstraintViolation<Product> violation : violations) { System.out.println("Validation error: " + violation.getMessage()); } } } }
These annotations work with Jakarta Bean Validation (JSR 380).
Make sure to have a validation provider like Hibernate Validator in your project.
Validation messages can be customized if needed.
@Min and @Max check that numbers stay within your set limits.
Use them on numeric fields to avoid invalid input.
They help keep your data clean and your app safe.
Practice
What is the main purpose of using @Min and @Max annotations in Spring Boot?
Solution
Step 1: Identify purpose of @Min and @Max
@Min and @Max set numeric limits on fields to ensure values stay within a range. Formatting dates, string length, and REST endpoints are unrelated.Final Answer:
To enforce minimum and maximum numeric values on fields -> Option BQuick Check:
@Min/@Max = numeric limits [OK]
- Confusing @Min/@Max with string length annotations
- Thinking they format dates
- Assuming they define REST endpoints
Which of the following is the correct way to apply @Min and @Max annotations on an integer field age to restrict it between 18 and 65?
public class Person {
// Which is correct?
private int age;
}Solution
Step 1: Verify annotation syntax
Annotations use parentheses with values, e.g., @Min(18), not =, {} or :. @Min(18) @Max(65) private int age; is correct; others invalid.Final Answer:
@Min(18) @Max(65) private int age; -> Option AQuick Check:
Annotations use (value) [OK]
- Using = or {} instead of () in annotations
- Forgetting to import javax.validation.constraints.*
- Placing annotations incorrectly outside the field
Given the following Spring Boot entity snippet, what will happen if score is set to 105?
public class GameScore {
@Min(0)
@Max(100)
private int score;
// getters and setters
}Solution
Step 1: Evaluate constraint for value 105
@Min(0) requires >= 0; @Max(100) requires <= 100. 105 > 100 violates @Max, triggering validation failure.Final Answer:
Validation will fail because 105 is greater than the max 100 -> Option AQuick Check:
Value > @Max = error [OK]
- Assuming values above max are accepted
- Confusing min and max roles
- Expecting runtime exceptions instead of validation errors
Identify the error in this code snippet that uses @Min and @Max:
public class Product {
@Min(1)
@Max(100)
private String quantity;
// getters and setters
}Solution
Step 1: Check field type compatibility
@Min/@Max apply only to numeric types (int, long, etc.), not String. quantity is String, causing validation error.Final Answer:
Annotations @Min and @Max cannot be applied to String fields -> Option DQuick Check:
@Min/@Max require numeric fields [OK]
- Applying @Min/@Max on non-numeric types
- Assuming @NotNull fixes type issues
- Ignoring type mismatch errors
You want to create a Spring Boot model field rating that only accepts values from 1 to 5 inclusive. Which of the following code snippets correctly enforces this using @Min and @Max?
Solution
Step 1: Select correct range and type
1-5 inclusive requires @Min(1) @Max(5) on int. public class Review { @Min(1) @Max(5) private int rating; } matches; @Min(0) allows 0, @Max(6) allows 6, String invalid.Final Answer:
public class Review { @Min(1) @Max(5) private int rating; } -> Option CQuick Check:
@Min(1)/@Max(5) on int [OK]
- Using wrong numeric ranges
- Applying annotations on String fields
- Setting min or max outside desired range
