我创建了一个将在 CSVMapper.schemaFor 中使用的 Java POJO:
@JsonPropertyOrder({ "mkg_brnd_id", "prnt_brnd_id" })
public class MKG_BRND {
public int mkg_brnd_id;
public int prnt_brnd_id;
}
但是,这些字段的 CSV 值可能是数字或空值。当它遇到空值时,我收到错误消息:
Can not construct instance of int from String value 'null': not a valid Integer value
我尝试使用 @JsonFormat 注释来指示应该允许数字或空值的 NUMBER:
@JsonPropertyOrder({ "mkg_brnd_id", "prnt_brnd_id" })
public class MKG_BRND {
@JsonFormat(shape=JsonFormat.Shape.NUMBER)
public int mkg_brnd_id;
@JsonFormat(shape=JsonFormat.Shape.NUMBER)
public int prnt_brnd_id;
}
但是没有喜悦,我仍然收到错误消息。
我还尝试了以下方法:
@JsonPropertyOrder({ "mkg_brnd_id", "prnt_brnd_id" })
public class MKG_BRND {
@JsonFormat(shape=JsonFormat.Shape.NUMBER)
public Integer mkg_brnd_id;
@JsonFormat(shape=JsonFormat.Shape.NUMBER)
public Integer prnt_brnd_id;
}
关于如何创建允许数值具有“null”值的模式的想法?
更新(11/26/2012 @ 11:56 am CST):
最终不得不重写 getter/setter 并自己操纵这些值:
@JsonPropertyOrder({ "mkg_brnd_id", "prnt_brnd_id" })
public class MKG_BRND {
@JsonFormat(shape=JsonFormat.Shape.NUMBER)
public Integer mkg_brnd_id;
@JsonFormat(shape=JsonFormat.Shape.NUMBER)
public Integer prnt_brnd_id;
public Integer getMkg_brnd_id() {
return mkg_brnd_id;
}
public void setMkg_brnd_id(String anIntValue) {
if ((C_NULL_UC.equals(anIntValue)) ||
(C_NULL_LC.equals(anIntValue))) {
this.mkg_brnd_id = null;
}
else {
this.mkg_brnd_id = new Integer(aIntValue);
}
}
}