Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON:

longkui / 2024-10-23 / 原文

原文链接:Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: – 每天进步一点点 (longkui.site)

 

调试springboot项目,返回数据的时候报错:

Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: java.time.LocalDateTime cannot be cast to java.lang.String; nested exception is com.fasterxml.jackson.databind.JsonMappingException: java.time.LocalDateTime cannot be cast to java.lang.String (through reference chain: java.util.ArrayList[0]->java.util.HashMap[“update_time”])]

这个报错主要是返回的数据类型不一致导致,详见下图:

可以看出,用HashMap<String,String>接收返回来的数据,但是数据中不都是String,还有Long、time类型,所以返回的时候会报错。

解决办法:

1.定义实体类和数据库字段做对应

2.继续偷懒,List<HashMap<String,String>>改成 List<HashMap<HashMap,HashMap>>形式。