Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在验证给定长度的BigDecimal的大小,而不是精度_Java_Bean Validation_Jsr - Fatal编程技术网

Java 正在验证给定长度的BigDecimal的大小,而不是精度

Java 正在验证给定长度的BigDecimal的大小,而不是精度,java,bean-validation,jsr,Java,Bean Validation,Jsr,我需要使用JSR验证器验证BigDecimal对象的长度。它最多应包含10个字符 Some valid examples: 123456789.0 12345.67890 12345.67 1.2345 Invalid examples: 123456789.0123 123.32131232 如何使用注释实现这一点?根据JSR文档,以下@Size注释适用于字符串对象 @Size(max = 10) @Column(name = "totalPrice") private BigDecima

我需要使用JSR验证器验证BigDecimal对象的长度。它最多应包含10个字符

Some valid examples:
123456789.0
12345.67890
12345.67
1.2345

Invalid examples:
123456789.0123
123.32131232
如何使用注释实现这一点?根据JSR文档,以下@Size注释适用于字符串对象

@Size(max = 10)
@Column(name = "totalPrice")
private BigDecimal totalPrice;
你可以试试

“@Digitsinteger=,fraction=或@DecimalMaxvalue=9999999999.999,message=十进制值不能超过9999999999”

这两者都应该起作用

如果您想知道如何使用这些URL,请使用以下URL

对于@位

对于@decimalmax


需要自定义约束。这可以大致如下所示:

注释:

@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = { BigDecimalLengthValidator.class})
public @interface BigDecimalLength {
    int maxLength();
    String message() default "Length must be less or equal to {maxLength}";
    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default { };
}

这应该满足基本需求,以便在属性文件等中进一步优化消息。请检查。

最多10个字符?我希望它使用JSR注释进行验证。不是以这种方式实现您自己的自定义验证约束,即您自己的注释和您自己的ConstraintValidator实现。
public class BigDecimalLengthValidator implements ConstraintValidator<BigDecimalLength, BigDecimal> {
    private int max;

    @Override
    public boolean isValid(BigDecimal value, ConstraintValidatorContext context) {
        return value == null || value.toString().length() <= max;
    }

    @Override
    public void initialize(BigDecimalLength constraintAnnotation) {
        this.max = constraintAnnotation.maxLength();
    }
}
@BigDecimalLength(maxLength = 3)
private BigDecimal totalPrice;