如何使用@DecimalMin和@DecimalMax验证List <bigdecimal>?

时间:2018-04-22 12:01:16

标签: java spring bean-validation hibernate-validator

在我的Spring项目中,我有一个POJO类,其中包含CMYK颜色的属性。 我希望这个属性由一个JSON数组表示,该数组恰好有4个浮点数。 每个号码必须介于0.01.0之间。 目前,我正在努力验证这个属性。

我已经进行了研究,发现@DecimalMin@DecimalMax 注释不能在Floatfloat上使用(请参阅this question的答案)。 因此,我已放弃List<Float>并改为使用List<BigDecimal>

这是我的精简版POJO课程:

public class Settings {

    @NotNull
    @Size(min = 4, max = 4)
    @DecimalMin("0")
    @DecimalMax("1")
    private List<BigDecimal> cmykColor;

    public List<BigDecimal> getCmykColor() {
        return cmykColor;
    }

    public void setCmykColor(List<BigDecimal> cmykColor) {
        this.cmykColor = cmykColor;
    }
}

这里是JUnit测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SettingsTest extends Assert {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private Validator validator; // from org.springframework.validation

    private Errors validate(String json) throws IOException {
        Settings settings = objectMapper.readValue(json, Settings.class);
        Errors errors = new BeanPropertyBindingResult(settings, "settings");
        validator.validate(settings, errors);
        return errors;
     }

    @Test
    public void testValid() throws IOException {
        String json = "{ \"cmykColor\": [0.5, 1.0, 0.3, 0.0] }";
        Errors errors = validate(json);
        assertEquals(0, errors.getErrorCount());
    }

    @Test
    public void testTooBig() throws IOException {
        String json = "{ \"cmykColor\": [0.5, 1.01, 0.3, 0.0] }";
        Errors errors = validate(json);
        assertEquals(1, errors.getErrorCount());    
    }

    @Test
    public void testTooMany() throws IOException {
        String json = "{ \"cmykColor\": [0.5, 1.0, 0.3, 0.0, 0.0] }";
        Errors errors = validate(json);
        assertEquals(1, errors.getErrorCount());
        assertNotNull(errors.getFieldError("cmykColor"));
    }
}

当我注释掉@DecimalMin("0")@DecimalMax("1")注释时, 所有测试用例都成功,当然除testTooBig测试用例外, 这将依赖于@DecimalMax约束。

但是当我添加@DecimalMin("0")@DecimalMax("1")注释时, 我在所有测试用例中遇到以下异常,说没有验证器 让DecimalMax验证List<BigDecimal>

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.DecimalMax' validating type 'java.util.List<java.math.BigDecimal>'. Check configuration for 'cmykColor'
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getExceptionForNullValidator(ConstraintTree.java:108)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:140)
    at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.validateConstraints(SimpleConstraintTree.java:55)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:73)
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.doValidateConstraint(MetaConstraint.java:127)
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:120)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:533)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:496)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:465)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:430)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:380)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:169)
    at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:104)
    at com.oce.spring.domain.SettingsTest.validate(SettingsTest.java:29)
    at com.oce.spring.domain.SettingsTest.testTooMany(SettingsTest.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    ...

那么,我应该如何使用@DecimalMin("0")@DecimalMax("1")List<BigDecimal>正确吗?

1 个答案:

答案 0 :(得分:2)

我们在Hibernate Validator 6.0.x中实现了容器元素约束。

因此升级到Hibernate Validator 6.0.x(6.0.9.Final是最新版本)和Bean Validation 2.0.1并执行如下操作:

@Size(...)
private List<@DecimalMin(...) @DecimalMax(...) BigDecimal> cmykColor;

注意,Hibernate Validator 6.0的groupId是org.hibernate.validator。