我正在使用Spring Data从MongoDB数据库检索文档。
我的模特班:
@Data
public class QuestionnaireItemNumber extends QuestionnaireItem {
private int min;
private int max;
private int value;
@Builder
public QuestionnaireItemNumber(String key, boolean required, String label, String description, int min, int max, int value) {
super(key, required, label, description);
this.min = min;
this.max = max;
this.value = value;
}
}
它表示将在前端呈现的Formcontrol项的Number字段。 min
max
和value
都可以为空。如果要从数据库中检索项目,则会得到以下堆栈:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate #############.domain.Questionnaire.QuestionnaireItemNumber using constructor public #############.domain.Questionnaire.QuestionnaireItemNumber(java.lang.String,boolean,java.lang.String,java.lang.String,int,int,int) with arguments age,true,age,null,null,null,null
<<irrelevant stack>>
Caused by: java.lang.IllegalArgumentException: Parameter min must not be null!
at org.springframework.util.Assert.notNull(Assert.java:198)
at ###########.domain.Questionnaire.QuestionnaireItemNumber_Instantiator_7q5eq0.newInstance(Unknown Source)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(ClassGeneratingEntityInstantiator.java:226)
我知道int不能为null,但是替代方法是什么?我应该使用java Integer
类吗?可以将databasevalue设置为“ NaN”或类似的值以获得期望的结果吗?