最近请求接口报了一个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为空,则异常,但是这个异常不会在方法执行过程中爆出,只会在前端获取时爆出。
至此,解决完毕