0
0
SpringbootHow-ToBeginner · 4 min read

How to Use @Min and @Max Annotations in Spring Boot

In Spring Boot, use @Min and @Max annotations on numeric fields to enforce minimum and maximum value constraints. These annotations work with the Bean Validation API and trigger validation errors if values fall outside the specified range.
📐

Syntax

The @Min and @Max annotations are used on numeric fields or parameters to specify the allowed minimum and maximum values respectively. Both take a single parameter which is the boundary value.

  • @Min(value): Ensures the number is not less than value.
  • @Max(value): Ensures the number is not greater than value.

They are part of the javax.validation.constraints package and require a validation framework like Hibernate Validator.

java
import javax.validation.constraints.Min;
import javax.validation.constraints.Max;

public class Example {
    @Min(1)
    @Max(10)
    private int number;

    // getters and setters
}
💻

Example

This example shows a Spring Boot REST controller that validates a request body with @Min and @Max annotations. If the input number is outside the range, Spring Boot returns a validation error automatically.

java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@RestController
@Validated
@RequestMapping("/api")
class NumberController {

    @PostMapping("/validate")
    public String validateNumber(@Valid @RequestBody NumberInput input) {
        return "Valid number: " + input.getNumber();
    }
}

class NumberInput {
    @Min(5)
    @Max(15)
    private int number;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}
Output
POST /api/validate with {"number":10} returns 200 OK with body "Valid number: 10" POST /api/validate with {"number":3} returns 400 Bad Request with validation error message
⚠️

Common Pitfalls

  • Not adding @Valid on the method parameter causes validation to be skipped.
  • Using @Min and @Max on non-numeric types like String will not work.
  • For floating point numbers, use @DecimalMin and @DecimalMax instead.
  • For validation errors to be returned properly, ensure spring-boot-starter-validation dependency is included.
java
/* Wrong: Missing @Valid, validation won't trigger */
@PostMapping("/wrong")
public String wrongValidate(@RequestBody NumberInput input) {
    return "Number: " + input.getNumber();
}

/* Right: Add @Valid to trigger validation */
@PostMapping("/right")
public String rightValidate(@Valid @RequestBody NumberInput input) {
    return "Number: " + input.getNumber();
}
📊

Quick Reference

Summary of key points for using @Min and @Max in Spring Boot:

  • Use on numeric fields or parameters to set minimum and maximum allowed values.
  • Annotate method parameters with @Valid to enable validation.
  • Include spring-boot-starter-validation in your project dependencies.
  • Use @DecimalMin and @DecimalMax for decimal numbers.

Key Takeaways

Use @Min and @Max to enforce numeric value limits in Spring Boot models or DTOs.
Always add @Valid on controller method parameters to trigger validation.
Include spring-boot-starter-validation dependency for validation support.
Use @DecimalMin and @DecimalMax for floating point numbers instead of @Min/@Max.
Validation errors automatically return 400 Bad Request with error details in Spring Boot.