spring boot的返回值里面含有net.sf.json.JSONObject 报错net.sf.json.JSONNull[“empty“])]

news/2024/4/28 3:12:22

一、过程

在对接第三的接口使,发现对方使用的json是net.sf.json.JSONObject。接口在返回值的时候就没有对其进行处理,直接返回了但是,后台也不报错,后端也没有收到响应值,只显示500的报错状态码。仔仔细细的看后台的日志发现:

2024-03-28 10:57:14.514  WARN 34128 --- [nio-8022-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver :Resolved [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.at21.sign2.util.ResultUtils["data"]-
>net.sf.json.JSONObject["dd"]->net.sf.json.JSONArray[1]->net.sf.json.JSONNull["empty"])]

这尼玛,报错就好好报错,你打个warn 是什么鬼,就不会error么!!!屮

二、复现bug

接口的部分代码

    @RequestMapping("test")public Object test() {JSONArray ja = new JSONArray();ja.add("aaa");ja.add(null);JSONObject js = new JSONObject();js.put("aa", 141);js.put("bb", null);js.put("cc", "");js.put("dd", ja);System.out.println(js.toString());return ResultUtils.success(js);}

打印的json数据是:  {"aa":141,"cc":"","dd":["aaa",null]}
问题1:bb是null,你™的吃了啊!!

问题2:数组里面有null spring boot响应就直接500了

三、解决

方法1:遍历去除数组中的null

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);

方法2:配置spring boot 的json格式的序列化

@Bean
public ObjectMapper objectMapper(){return new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

方法3:不要使用 net.sf.json.JSONObject (垃圾)

建议使用阿里的 com.alibaba.fastjson.JSONObject

        <dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.83</version></dependency>


http://wed.xjx100/news/1263617.html

相关文章

【MATLAB源码-第171期】基于matlab的布谷鸟优化算法(COA)无人机三维路径规划,输出做短路径图和适应度曲线

操作环境&#xff1a; MATLAB 2022a 1、算法描述 布谷鸟优化算法&#xff08;Cuckoo Optimization Algorithm, COA&#xff09;是一种启发式搜索算法&#xff0c;其设计灵感源自于布谷鸟的独特生活习性&#xff0c;尤其是它们的寄生繁殖行为。该算法通过模拟布谷鸟在自然界中…

conda安装GPU版pytorch,但是安装的是cpu的

conda安装GPU版pytorch&#xff0c;但是安装的是cpu的 安装命令 conda install pytorch torchvision torchaudio pytorch-cuda11.7 -c pytorch -c nvidia安装结果 解决办法一&#xff08;别的博客建议&#xff0c;这里不适合我&#xff09; 卸载pytorch-mutex或者卸载cpuonly …

MySQL创建表:练习题

练习题&#xff1a; 创建一个名为"students"的数据库&#xff0c;并切换到该数据库。 在"students"数据库中创建一个名为"grades"的表&#xff0c;包含以下字段&#xff1a; id: 整数类型 name: 字符串类型&#xff0c;学生姓名 subject: 字符串…

第十四届蓝桥杯JavaA组省赛真题 - 互质数的个数

解题思路&#xff1a; 快速幂 欧拉函数 快速幂比较常见于数据较大的取模场景&#xff0c;欧拉函数感觉还是有点抽象 注意&#xff1a; 取模的时候就不要简写了&#xff0c;例如&#xff1a;res res * a % mod;不要写成res * a % mod; import java.util.Scanner;public c…

记录minio、okhttp、kotlin一连环的版本冲突问题

问题背景 项目中需要引入minio&#xff0c;添加了如下依赖 <dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.5.2</version></dependency> 结果运行报错&#xff1a; Caused by: java.la…

flink on yarn-per job源码解析、flink on k8s介绍

Flink 架构概览–JobManager JobManager的功能主要有: 将 JobGraph 转换成 Execution Graph,最终将 Execution Graph 拿来运行Scheduler 组件负责 Task 的调度Checkpoint Coordinator 组件负责协调整个任务的 Checkpoint,包括 Checkpoint 的开始和完成通过 Actor System 与 …

Git命令上传本地项目至github

记录如何创建个人仓库并上传已有代码至github in MacOS环境 0. 首先下载git 方法很多 这里就不介绍了 1. Github Create a new repository 先在github上创建一个空仓库&#xff0c;用于一会儿链接项目文件&#xff0c;按照自己的需求设置name和是否private 2.push an exis…

vue组件弹窗窗

import { createApp } from vue; import { Popup } from vant;const app createApp(); app.use(Popup); 有这样的一个效果 在手机上很美观 不错 建议大家使用