0
0
Spring Bootframework~5 mins

@Min, @Max for numeric constraints in Spring Boot

Choose your learning style9 modes available
Introduction

@Min and @Max help make sure numbers stay within limits you set. This stops wrong or unexpected numbers from causing problems.

When you want to check that a user's age is not less than 18 or more than 100.
When you need to limit a product quantity between 1 and 10 in an order form.
When you want to ensure a rating score is between 0 and 5.
When you want to validate numeric input in a form before saving to the database.
Syntax
Spring Boot
@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.

Examples
This means age cannot be less than 18.
Spring Boot
@Min(18)
private int age;
This means age cannot be more than 100.
Spring Boot
@Max(100)
private int age;
This means rating must be between 1 and 5 inclusive.
Spring Boot
@Min(1)
@Max(5)
private int rating;
Sample Program

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.

Spring Boot
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());
            }
        }
    }
}
OutputSuccess
Important Notes

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.

Summary

@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.