返回值JSON串化失败,HttpMessageNotWritableException异常解决办法

本文详细解析了在Spring框架中,使用Jackson进行JSON序列化时遇到的NullPointerException异常,并提供了检查数据完整性和使用@JsonIgnoreProperties注解的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

controller将返回参数json化后,返回给前端的时候,报异常org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.baomidou.mybatisplus.extension.api.R["data"]->java.util.ArrayList[3]->com.sunflower.sunfloweresreport.modules.system.entity.SysMenuEntity["leaf"])

还要继续看日志:

Caused by: java.lang.NullPointerException: null
    at com.sunflower.sunfloweresreport.modules.system.entity.SysMenuEntity.getLeaf(SysMenuEntity.java:100) ~[classes/:na]

原因已经很明确了,该SysMenuEntity类leaf属性是null,导致json化得时候,fasterxml -jackson 将对象转换为json时报错了。

解决方案:

1.首先查看数据是否后问题,因为表结构不一致,数据是否保存完整

2.添加注释@JsonIgnoreProperties,空的时候不序列化

如:

@JsonIgnoreProperties

public class SysMenuEntity extends AdminSuperEntity implements Serializable{

}

@Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 创建一个FastJsonHttpMessageConverter实例,用于JSON数据的转换 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); // 创建并配置一个FastJsonConfig实例,用于定制JSON数据的处理规则 FastJsonConfig fastJsonConfig = new FastJsonConfig(); // 设置JSON数据的序列特性,此处为格式输出,提高可读性 fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 获取全局的序列配置实例 SerializeConfig serializeConfig = SerializeConfig.globalInstance; // 将BigInteger、Long转为String类型 serializeConfig.put(BigInteger.class, ToStringSerializer.instance); serializeConfig.put(Long.class, ToStringSerializer.instance); serializeConfig.put(Long.TYPE, ToStringSerializer.instance); // 配置LocalDateTime类型的数据转换为字符类型,以适应JSON格式 serializeConfig.put(LocalDateTime.class, ToStringSerializer.instance); // 将自定义的序列配置应用到FastJsonConfig中 fastJsonConfig.setSerializeConfig(serializeConfig); // 将配置好的FastJsonConfig应用到FastJsonHttpMessageConverter中 fastConverter.setFastJsonConfig(fastJsonConfig); // 返回包含配置好的FastJsonHttpMessageConverter的HttpMessageConverters return new HttpMessageConverters(fastConverter); }为什么我不配置以上的消息转换 @PostMapping("test") public void test(@RequestBody Map deviceReported){ System.out.println("map = " + deviceReported); }这个接口调用会出现2025-03-29 18:07:30.897 WARN 17460 --- [ XNIO-1 task-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported] 2025-03-29 18:07:30.905 WARN 17460 --- [ XNIO-1 task-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap]这个问题
最新发布
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值