Cannot construct instance of `java.time.LocalDateTime`

異常信息:(很很明顯是反序列化錯誤)

Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-10-21 13:57:38')
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: java.util.ArrayList[0]->com.construn.vehicle.tmrp.entity.IncomeSummaryRepInfo["createTime"]) 
java.lang.IllegalArgumentException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-10-21 13:57:38')
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: java.util.ArrayList[0]->com.construn.vehicle.tmrp.entity.IncomeSummaryRepInfo["createTime"])

錯誤場景:數據庫createTime字段爲datetime類型;實體類中爲LocalDateTime類型;返回給前端,修改的時候,前端再返回來,就報了這個錯誤。(說明:我這裏,前端穿的是一個實體類list;在controller中用map接收的)

解決:我用的 ObjectMapper轉的list

List<xxxx> list1 = (List<xxxx>) map.get("list");
ObjectMapper mapper = new ObjectMapper();
//序列化的時候序列對象的所有屬性
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.registerModule(new JavaTimeModule());
List<xxxx> list = mapper.convertValue(list1, new TypeReference<List<xxxx>>() { });

主要是紅色兩行,其它的都是把前端傳的值轉換成list

 

說明:該場景只是本人遇到的,在對應的方法類轉換,這有可能不適用您,我是偷懶,直接在方法中寫,如果不實用,我想您可能要寫一個配置文件類了;

 

 

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