2012-11-26 46 views
1

我創建一個Java POJO將在CSVMapper.schemaFor使用:CsvSchema和NUMBER

@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; 
} 

的思考?

更新(2012年11月26日@上午11:56 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); 
    } 
} 
} 

回答

0

INT是一種基本類型,無論您將什麼註釋添加到類中,都不能設置爲null。如果這些屬性確實可能爲空,則應將它們從int更改爲Integer

+0

是的,我知道,這些值可能與Integer一樣(我嘗試過),但我仍然得到相同的錯誤。這個問題似乎是讓解析引擎認識到* .csv中的null值是一個特殊的值而不是字符串'null',並且在Integer的情況下,將它呈現爲null。 –

+0

如果我查看CsvSchema.ColumnType的JavaDoc,它指出NUMBER:Value應該是一個數字,但文字「null」,「true」和「false」也可以理解。我只是在努力弄清楚如何讓解析器將值作爲NUMBER來處理。 –

+0

@Dutchman明白了。我誤解了。那時我不知道。告訴你什麼,更新你的問題,你嘗試了整數,但它沒有奏效。然後我會在幾分鐘內刪除我的答案。如果您的問題未得到解答,您可能會採取更多措施。 –

相關問題