一、背景

近期在用 javax.validation 做校验的时候发现没有对 BigDecimal 类型进行校验的注解,便自己动手写了一个。

二、定义注解及校验类

2.1 注解类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Documented
@Constraint(validatedBy = BigDecimalRangeValidator.class)
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BigDecimalRange {
double min() default Double.MIN_VALUE;

double max() default Double.MAX_VALUE;

String message() default "Validation failed";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}

2.2 校验类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class BigDecimalRangeValidator implements ConstraintValidator<BigDecimalRange, BigDecimal> {

private BigDecimal minValue;
private BigDecimal maxValue;

@Override
public void initialize(BigDecimalRange constraintAnnotation) {
minValue = new BigDecimal(constraintAnnotation.min());
maxValue = new BigDecimal(constraintAnnotation.max());
}

@Override
public boolean isValid(BigDecimal value, ConstraintValidatorContext constraintValidatorContext) {
if (value == null) {
return true;
}
return value.compareTo(minValue) >= 0 && value.compareTo(maxValue) <= 0;
}
}

三、使用注解

1
2
3
4
5
6
7
8
public class Example {

/**
* 测试字段
*/
@BigDecimalRange(min = 0.1, max = 9.9, message = "测试字段的范围为0.1~9.9")
private BigDecimal testValue;
}