Java Bean Validation之Integer、Long、Double类型字段验证方案咨询(Spring Boot场景)
Let's break down your questions clearly, since you want to stick with Long and Double instead of switching to BigDecimal for your existing codebase.
1. Validating Long Type Fields
Native Validation Annotations
First, if your bean field is already declared as a Long (wrapper type) or long (primitive), Spring MVC will automatically handle type conversion during request binding. If the input isn't a valid integer (like letters or decimals), it'll throw a TypeMismatchException and return a 400 Bad Request out of the box—no extra annotations needed for basic type checks.
For stricter control (like enforcing value ranges or validating string inputs that need conversion to Long), use these annotations:
@Digits(integer = 19, fraction = 0): Ensures the input has at most 19 integer digits (matches the maximum length of aLong, which ranges from-9223372036854775808to9223372036854775807).@Min(value = Long.MIN_VALUE)and@Max(value = Long.MAX_VALUE): Explicitly enforces the numeric range of aLong.
Regular Expression for Long Validation
If you're receiving the input as a String (and need to validate before converting to Long), use this regex to match valid integer strings that fit within Long's format:
^-?[0-9]{1,19}$
^-?: Allows an optional negative sign.[0-9]{1,19}: Matches 1 to 19 digits (covers the full length of validLongvalues).
Note: The regex only validates the format. You'll still need @Min and @Max to ensure the numeric value doesn't exceed Long's bounds (since a 19-digit number could be larger than Long.MAX_VALUE).
Example Bean for Long Validation:
public class LongValidationRequest { // For Long type field @NotNull @Digits(integer = 19, fraction = 0) @Min(Long.MIN_VALUE) @Max(Long.MAX_VALUE) private Long validLong; // For String type field (to convert to Long later) @NotNull @Pattern(regexp = "^-?[0-9]{1,19}$") private String longAsString; // Getters and setters }
2. Validating Double Type Fields
Native Validation Annotations
Similar to Long, if your bean field is declared as Double or double, Spring MVC will handle basic type conversion errors automatically. For stricter validation:
@Digits(integer = 16, fraction = 15): Accounts forDouble's precision (it can represent up to ~17 significant digits). Adjust theintegerandfractionvalues based on your specific use case.@DecimalMinand@DecimalMax: Use these to define acceptable numeric ranges (note: these accept string values to avoid floating-point precision issues, e.g.,@DecimalMin("0.0")).
Regular Expression for Double Validation
If you're validating string inputs that need to become Double, use one of these regex patterns based on your needs:
Option 1: Supports standard decimal format (no scientific notation)
^-?([0-9]+(\.[0-9]+)?|[0-9]*\.[0-9]+)$
- Matches values like
123,-45.67,.89,0.123
Option 2: Supports standard format + scientific notation (e.g., 1.23e4, -5.6E-7)
^-?([0-9]+(\.[0-9]+)?|[0-9]*\.[0-9]+)([eE][+-]?[0-9]+)?$
Example Bean for Double Validation:
public class DoubleValidationRequest { // For Double type field @NotNull @Digits(integer = 16, fraction = 15) @DecimalMin("-1.7976931348623157E308") // Double.MIN_VALUE as string @DecimalMax("1.7976931348623157E308") // Double.MAX_VALUE as string private Double validDouble; // For String type field (supports scientific notation) @NotNull @Pattern(regexp = "^-?([0-9]+(\\.[0-9]+)?|[0-9]*\\.[0-9]+)([eE][+-]?[0-9]+)?$") private String doubleAsString; // Getters and setters }
Quick Reminder
If you're using the native wrapper types (Long, Double) directly in your bean, the built-in Spring type conversion will handle most invalid input cases. Use the annotations and regex patterns above only when you need to validate string inputs or enforce stricter range/format rules.
内容的提问来源于stack exchange,提问作者ban29

