记录一次HttpMessageNotWritableException异常

本文深入解析了在使用Spring框架时,从前端请求数据导致的HttpMessageNotWritableException异常,特别是当对象属性为空时引发的NullPointerException。通过分析重写get方法的场景,阐述了异常的根本原因,并提供了具体的解决方案。

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

最近请求接口报了一个org.springframework.http.converter.HttpMessageNotWritableException

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.anxinyiheng.commons.entity.response.ResultVO["data"]->com.anxinyiheng.asset.service
 .address.resource.entity.po.EditAddressResourceEchoPO["editAddressResourceRelationEch
 oList"]->java.util.ArrayList[0]->com.anxinyiheng.asset.service.address.resource.entity.po.EditA
 ddressResourceRelationEchoPO["content"])

从前端请求至Controller-》再到service-》再到数据库-》获取到数据返回至service-》再到Controller。都不报错,但是从前端一走完就报这个错误,很是纳闷,最后终于找出原因,原因如下

public String getContent() {
    //如果类型为1 为ip地址 2 为子网掩码
    if( this.type == 1){
      content = this.getIpAddress();
    }
    if( this.type == 2){
      content = this.getIpAddress()+"/"+this.getIpAddressEnd();
    }
    return content;
  }

我重写了其中一个字段的get方法,大家都知道前端在获取数据时,其实是调用了咱们的get方法,那么这时,问题就出现了,如果我type为空,则异常,但是这个异常不会在方法执行过程中爆出,只会在前端获取时爆出。

至此,解决完毕

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值