springboot返回值转成JSONString的处理方式
作者:wz_ling1991 时间:2022-09-27 12:04:46
返回值转成JSONString的处理
主要需求描述
有些返回值中的null需要转换成“”或[],另外有些返回值需要把null给屏蔽掉。
解决方法
一(很lowb)
所有的返回值均采用string,在返回值之前,自己特殊处理成JSONString,然后返回给前端即可。
二(比较正统)
通过配置FastJsonConfig,配置类如下:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//1.定义一个convert转换消息对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2.添加fastjson的配置信息,比如:是否要格式化返回json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteNullListAsEmpty
);
PropertyFilter propertyFilter = new PropertyFilter() {
@Override
public boolean apply(Object object, String name, Object value) {
if (object instanceof MessageFor841 && value == null) {
return false;
}
return true;
}
};
fastJsonConfig.setSerializeFilters(propertyFilter);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
}
这里解释一下PropertyFilter类中的apply方法,
首先3个参数:
object
:可以理解成需要转成json的对象name
:字段名(属性名)value
:字段值(属性值)
返回值是一个boolean值,为true时,则对应转成成jsonstring,为false时,则过滤掉。
这里类型为MessageFor841且它的属性字段为null时返回false,表示此字段过滤掉。
其他类型null字段转成""或[]
JSON.toJSONString
后端想将HashMap类型数据返回成json字符串
即导入并使用JSON.toJSONString
使用前提
pom.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
使用页需自己写入下列语句,否则无法自行导入
import com.alibaba.fastjson.JSON;
使用
HashMap<String, Object> data = new HashMap<>();
List<MainMenu> mainMenus = menuDao.getMainMenus();
data.put("data",mainMenus);
data.put("status",200);
String data_json = JSON.toJSONString(data);
System.out.println("成功访问!!!");
return data_json;
来源:https://blog.csdn.net/wz_ling1991/article/details/84653782
标签:springboot,返回值,JSONString
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
多线程(多窗口卖票实例讲解)
2021-09-02 02:47:26
![](https://img.aspxhome.com/file/2023/9/61159_0s.png)
简单记事本java源码实例
2023-11-26 02:03:17
![](https://img.aspxhome.com/file/2023/4/59744_0s.jpg)
实例解析JAVA中代码的加载顺序
2021-10-26 14:57:22
![](https://img.aspxhome.com/file/2023/8/61038_0s.png)
彻底掌握C语言strcpy函数的用法
2023-07-03 07:21:18
![](https://img.aspxhome.com/file/2023/4/103104_0s.png)
Spring实战之SpEl语法实例详解
2023-09-18 07:56:03
java中使用数组进行模拟加密的方法
2023-11-18 15:37:39
解决Android软键盘弹出覆盖h5页面输入框问题
2023-06-19 11:33:24
![](https://img.aspxhome.com/file/2023/7/83547_0s.jpg)
ArrayList和LinkedList的区别、扩容机制以及底层的实现方式
2023-11-27 01:26:57
![](https://img.aspxhome.com/file/2023/9/60279_0s.png)
Java设计模式之备忘录模式
2023-08-24 06:17:05
Java 数据结构之堆的概念与应用
2023-11-24 05:36:18
![](https://img.aspxhome.com/file/2023/9/59339_0s.png)
Java解析调用webservice服务的返回XML串详解
2023-11-07 02:42:01
Java Thread之Sleep()使用方法及总结
2023-11-16 10:38:35
![](https://img.aspxhome.com/file/2023/4/59014_0s.png)
SpringBoot异步调用方法并接收返回值
2023-08-16 22:33:49
![](https://img.aspxhome.com/file/2023/5/58055_0s.jpg)
java实现上传文件类型检测过程解析
2023-11-09 12:04:21
Java基础:流Stream详解
2023-11-29 06:11:14
![](https://img.aspxhome.com/file/2023/6/60086_0s.jpg)
java高并发ScheduledThreadPoolExecutor与Timer区别
2023-08-11 03:08:29
Java实现简单邮件发送功能
2023-08-15 00:53:23
java导出数据库的全部表到excel
2023-11-25 09:38:33
C语言预处理预编译命令及宏定义详解
2023-06-18 16:28:06
![](https://img.aspxhome.com/file/2023/3/83503_0s.png)
Java利用POI读取、写入Excel的方法指南
2023-11-23 15:39:23
![](https://img.aspxhome.com/file/2023/9/59899_0s.png)