ObjectMapper的坑

2017.07.07

報錯信息

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "conditionQuantity" (class objectMapper.NewInteger), not marked as ignorable (7 known properties: "reduceFee", "perReduceFee", "priority", "match", "reduceDiscount", "conditionFee", "per"])
 at [Source: [{"conditionQuantity":30,"reduceDiscount":90.5,"priority":1}]; line: 1, column: 25] (through reference chain: java.util.ArrayList[0]->objectMapper.NewInteger["conditionQuantity"])

一直顯示的錯誤是沒有匹配的conditionQuantity字段

Class源碼

public class NewInteger implements Serializable{

    private static final long serialVersionUID = 4878434867152140447L;

    private Long conditionFee;      

    private Long per;               

    private Long perReduceFee;       

    private Integer conditionQuantity;  

    private Long reduceFee;         

    private Integer reduceDiscount;     

    private Integer priority;           

    private Boolean isMatch;           


}

很明顯class類中是有conditionQuantity字段的,于是思考過去成功的案例鞭铆。

發(fā)現(xiàn)唯一失敗的可能就是沒有設(shè)置setter和getter方法或衡。

于是修改class類的代碼。

修改后Class

public class NewInteger implements Serializable{

    private static final long serialVersionUID = 4878434867152140447L;

    private Long conditionFee;     

    private Long per;                

    private Long perReduceFee;       

    private Integer conditionQuantity;  

    private Long reduceFee;         

    private Integer reduceDiscount;    

    private Integer priority;           

    private Boolean isMatch;          


    public Long getConditionFee() {
        return conditionFee;
    }

    public void setConditionFee(Long conditionFee) {
        this.conditionFee = conditionFee;
    }

    public Long getPer() {
        return per;
    }

    public void setPer(Long per) {
        this.per = per;
    }

    public Long getPerReduceFee() {
        return perReduceFee;
    }

    public void setPerReduceFee(Long perReduceFee) {
        this.perReduceFee = perReduceFee;
    }

    public Integer getConditionQuantity() {
        return conditionQuantity;
    }

    public void setConditionQuantity(Integer conditionQuantity) {
        this.conditionQuantity = conditionQuantity;
    }

    public Long getReduceFee() {
        return reduceFee;
    }

    public void setReduceFee(Long reduceFee) {
        this.reduceFee = reduceFee;
    }

    public Integer getReduceDiscount() {
        return reduceDiscount;
    }

    public void setReduceDiscount(Integer reduceDiscount) {
        this.reduceDiscount = reduceDiscount;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    public Boolean getMatch() {
        return isMatch;
    }

    public void setMatch(Boolean match) {
        isMatch = match;
    }
}

ObjectMapper解析代碼

public class ObejctMapperTest {


    public List<NewInteger> parse() throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();

        String json = "[{\"conditionQuantity\":30,\"reduceDiscount\":90.5,\"priority\":1}]";
        return objectMapper.readValue(json, new TypeReference<List<NewInteger>>(){});


    }

    public static void main(String[] args) {
        ObejctMapperTest run = new ObejctMapperTest();
        try {
            System.out.println("before parse");
            List<NewInteger> result = run.parse();
            System.out.println(result.get(0).getReduceDiscount());
        } catch (IOException e) {
            System.out.println("catch parse error");
            System.out.println(e.toString());
        }
    }
}

[解析結(jié)果]

解析結(jié)果

從結(jié)果來看车遂,很明顯踩到另一個坑了封断。
在拿Integer類型的字段reduceDiscount時,相對的實(shí)際參數(shù)是90.5舶担,這種情況下缤苫,objectMapper并不會報錯猴凹,而是直接精度丟失了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市丁溅,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌钥星,老刑警劉巖述召,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異译断,居然都是意外死亡授翻,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進(jìn)店門孙咪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來堪唐,“玉大人,你說我怎么就攤上這事该贾「嵫睿” “怎么了?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵杨蛋,是天一觀的道長兜材。 經(jīng)常有香客問我理澎,道長,這世上最難降的妖魔是什么曙寡? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任糠爬,我火速辦了婚禮,結(jié)果婚禮上举庶,老公的妹妹穿的比我還像新娘执隧。我一直安慰自己,他們只是感情好户侥,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布镀琉。 她就那樣靜靜地躺著,像睡著了一般蕊唐。 火紅的嫁衣襯著肌膚如雪屋摔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天替梨,我揣著相機(jī)與錄音钓试,去河邊找鬼。 笑死副瀑,一個胖子當(dāng)著我的面吹牛弓熏,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播糠睡,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼挽鞠,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了铜幽?” 一聲冷哼從身側(cè)響起滞谢,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎除抛,沒想到半個月后狮杨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡到忽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年橄教,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片喘漏。...
    茶點(diǎn)故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡护蝶,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出翩迈,到底是詐尸還是另有隱情持灰,我是刑警寧澤,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布负饲,位于F島的核電站堤魁,受9級特大地震影響喂链,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜妥泉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一椭微、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧盲链,春花似錦蝇率、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至悠轩,卻和暖如春间狂,著一層夾襖步出監(jiān)牢的瞬間攻泼,已是汗流浹背火架。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留忙菠,地道東北人何鸡。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像牛欢,于是被迫代替她去往敵國和親骡男。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉傍睹,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 1,715評論 0 9
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,127評論 25 707
  • 有多少真心 你誤以為是玩笑 所以愉快的交談已是過往 我們還是最好的朋友 游戲青春 自認(rèn)為隔盛,我們所認(rèn)為的 現(xiàn)在 想起...
    貓眼小先生閱讀 205評論 0 0
  • “二哥”這個稱呼是朋友聚會的時候排下來的,叫的久了拾稳,也就自然習(xí)慣了吮炕。 仔細(xì)想想也很有意思,似乎從小到大就沒被排過大...
    二哥的食堂閱讀 987評論 2 9