springboot 接口返回数据时 net.sf.json.JSONNull[“empty“]) 异常处理
Posted 向天再借500年V
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot 接口返回数据时 net.sf.json.JSONNull[“empty“]) 异常处理相关的知识,希望对你有一定的参考价值。
springboot 接口返回数据时 net.sf.json.JSONNull[“empty”]) 异常处理
@ResetController返回数据时出现异常
Could not write JSON: Object is null; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Object is null (through reference chain: com.banxue.common.entity.ReturnEntity["data"]->net.sf.json.JSONObject["inviteRecord"]->net.sf.json.JSONArray[0]->net.sf.json.JSONObject["exchange_id"]->net.sf.json.JSONNull["empty"])
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Object is null; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Object is null (through reference chain: com.banxue.common.entity.ReturnEntity["data"]->net.sf.json.JSONObject["inviteRecord"]->net.sf.json.JSONArray[0]->net.sf.json.JSONObject["exchange_id"]->net.sf.json.JSONNull["empty"])
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:293)
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:106)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:231)
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:203)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:113)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
解决方法1:配置全局 fastJsonConfig
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters()
// 1.定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// 3.配置选用
fastJsonConfig.setSerializerFeatures(
// 输出格式整理
SerializerFeature.PrettyFormat,
// 输出key时是否使用双引号
SerializerFeature.QuoteFieldNames,
// 是否输出值为null的字段
SerializerFeature.WriteMapNullValue,
// List字段如果为null,输出为[],而非null
SerializerFeature.WriteNullListAsEmpty,
// 字符类型字段如果为null,输出为”“,而非null
SerializerFeature.WriteNullStringAsEmpty,
// 数字类型字段如果为null,输出为0,而非null
SerializerFeature.WriteNullNumberAsZero);
// 4.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 5.将converter赋值给HttpMessageConverter
HttpMessageConverter<?> converter = fastConverter;
// 6.返回HttpMessageConverters对象
return new HttpMessageConverters(converter);
解决方法2:定向移除 JSONNull
JSONObject json2 = new JSONObject();
JSONObject xxxxxx = (JSONObject) json.get("xxxxxx");
Iterator<Map.Entry<String, Object>> it = xxxxxx.entrySet().iterator();
while (it.hasNext())
Map.Entry<String, Object> next = it.next();
if(!(next.getValue() instanceof JSONNull))
json2.put(next.getKey(),next.getValue());
json.put("xxxxxx", json2);
以上是关于springboot 接口返回数据时 net.sf.json.JSONNull[“empty“]) 异常处理的主要内容,如果未能解决你的问题,请参考以下文章
net.sf.jasperreports.engine.JRException如何解决
Mybatis - NoSuchMethodError: net.sf.jsqlparser.statement.select.SetOperationList.getSelects()Ljava/
Mybatis - NoSuchMethodError: net.sf.jsqlparser.statement.select.SetOperationList.getSelects()Ljava/(
Mybatis - NoSuchMethodError: net.sf.jsqlparser.statement.select.SetOperationList.getSelects()Ljava/(