1

我创建了一个将在 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);
    }
}
}
4

2 回答 2

0

Sounds like something to suggest as an RFE at https://github.com/FasterXML/jackson-dataformat-csv/issues -- sounds like a good improvement for other users.

于 2012-12-05T06:17:32.633 回答
0

int 是原始类型,无论您向类添加什么注释,都不能设置为 null。如果这些属性确实可能为 null,则应将它们从 更改intInteger

于 2012-11-26T16:57:04.043 回答