記一次 jackson json轉實體 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException

最近在學習 SpringCloud微服務實戰一書中,也算是重新溫習下SpringCloud的知識點。

 

ObjectMapper mapper = new ObjectMapper();
        WeatherResponse weather = null;

        try {
            weather = mapper.readValue(strBody, WeatherResponse.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

 

然後在這段代碼中出現了  com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException ; 後來百度查看,原來我是習慣性的用錯了字段名字導致的。

由於bean(也就是上面的WeatherResponse)中缺少json的某個字段屬性引起 或者 字段名字錯誤對不上導致的。

1 : 檢查如果是名字不一樣的錯誤導致,就對名字進行修改。

2 :   Bean中 添加註解@JsonIgnoreProperties(ignoreUnknown = true)  (這不是因爲字段名問題導致的)

3 :   mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章