java bigdecimal min_java
在我的Spring項目中,我有一個POJO類,該類具有CMYK顏色的屬性。 我希望此屬性由具有4個浮點數的JSON數組表示。 每個數字必須在0.0到1.0之間。 目前,我正在努力驗證此屬性。
我已經進行了研究,發現@DecimalMin和@DecimalMax批注不能在Float或float (請參閱此問題的答案)。 因此,我已經放棄了List并改用List 。
這是我精簡的POJO類:
public class Settings {
@NotNull
@Size(min = 4, max = 4)
@DecimalMin("0")
@DecimalMax("1")
private List cmykColor;
public List getCmykColor() {
return cmykColor;
}
public void setCmykColor(List 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 。
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.DecimalMax' validating type 'java.util.List'. 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)
...
那么,我應該如何在List正確使用@DecimalMin("0")和@DecimalMax("1") ?
總結
以上是生活随笔為你收集整理的java bigdecimal min_java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java程序编写九九乘法表_用面向对象的
- 下一篇: Java动物类enjoy方法打印_Jav