最近在學習 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);