报错如下:
![解决报错:No converter for [xxx] with preset Content-Type ‘null‘和Could not find acceptable representation,第1张 解决报错:No converter for [xxx] with preset Content-Type ‘null‘和Could not find acceptable representation,第1张](/upload/website_attach/202312/1_XYTNJ6ZF6VDDQVXE.jpeg)
![解决报错:No converter for [xxx] with preset Content-Type ‘null‘和Could not find acceptable representation,第2张 解决报错:No converter for [xxx] with preset Content-Type ‘null‘和Could not find acceptable representation,第2张](/upload/website_attach/202312/1_E4DA7SS9TEYCUNPJ.jpeg)
![解决报错:No converter for [xxx] with preset Content-Type ‘null‘和Could not find acceptable representation,第3张 解决报错:No converter for [xxx] with preset Content-Type ‘null‘和Could not find acceptable representation,第3张](/upload/website_attach/202312/1_FQ6D6T5A9VMJRSGA.jpeg)
从表面上看:似乎是因为没有序列号转换工具converter的原因,但是springboot实际已经自带jackson序列号转换工具(在spring-boot-starter-web/spring-boot-starter-json包下)。
![解决报错:No converter for [xxx] with preset Content-Type ‘null‘和Could not find acceptable representation,第4张 解决报错:No converter for [xxx] with preset Content-Type ‘null‘和Could not find acceptable representation,第4张](/upload/website_attach/202312/1_5CGZK5669Y4JDTAR.jpeg)
所以明显不是这个原因。那么真正的原因是什么?
实际上,报这个异常跟序列化的原理有关,不论fastjson/jackson在序列化的时候,都是利用反射找到对象类的所有get方法,获取方法名称,然后首字母小写,作为json的每个key值,而get方法的返回值作为value, 最后添加到json中。
所以,解决办法:给实体类添加好getter方法就行了
![解决报错:No converter for [xxx] with preset Content-Type ‘null‘和Could not find acceptable representation,第5张 解决报错:No converter for [xxx] with preset Content-Type ‘null‘和Could not find acceptable representation,第5张](/upload/website_attach/202312/1_ZVUWKVFSBZ6BEDQH.jpeg)