Spring Boot Validation with @Min and @Max Annotations

Hi everyone 👋 Continuing the Spring Boot Validation Series Part 29 👇 📌 Validation Annotations – @Min & @Max @Min and @Max are used to validate numeric values 👇 🔹 Why do we use them? Sometimes we need to restrict numbers to a certain range. Age should be at least 18 Quantity should not exceed 100 @Min and @Max help enforce these rules automatically. 🔹 Simple Example public class User { @Min(18) private int age; @Max(100) private int score; } @PostMapping("/users") public String createUser(@Valid @RequestBody User user) { return "User created"; } 👉 If age < 18 or score > 100 → validation fails ❌ 🔹 Important Points Works with numeric types: int, long, double, BigDecimal Often combined with @NotNull to ensure value is provided @NotNull @Min(1) @Max(100) private Integer quantity; 🔹 In simple words @Min → number must be greater than or equal to specified value @Max → number must be less than or equal to specified value 👉 🧠 Quick Understanding @Min(value) → minimum allowed value @Max(value) → maximum allowed value Works with all numeric types Often combined with @NotNull #SpringBoot #Java #Validation #MinMaxAnnotation #BackendDevelopment #LearningInPublic

To view or add a comment, sign in

Explore content categories