在我的Spring项目中,我有一个POJO类,该类具有CMYK颜色的属性。我希望此属性由具有4个浮点数的JSON数组表示。每个数字都必须在0.0和之间1.0。目前,我正在努力验证此属性。
0.0
1.0
我已经进行了研究,发现@DecimalMin和@DecimalMax 注释不能在Float或上使用float(请参阅此问题的答案)。因此,我已经放弃了List<Float>,List<BigDecimal>取而代之。
@DecimalMin
@DecimalMax
Float
float
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")and @DecimalMax("1")注释时,所有测试用例都成功,当然testTooBig测试用例除外,后者将依赖于@DecimalMax约束。
@DecimalMin("0")
@DecimalMax("1")
testTooBig
但是,当我添加@DecimalMin("0")和@DecimalMax("1")注释时,在 所有 测试用例上 都会 得到以下异常,表示没有可用于DecimalMax验证的验证器List<BigDecimal>。
DecimalMax
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>正确?
我们在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。