SpringBoot项目中处理返回json的null值(springboot项目为例)
作者:ryantom 时间:2023-10-26 04:49:06
在后端数据接口项目开发中,经常遇到返回的数据中有null值,导致前端需要进行判断处理,否则容易出现undefined的情况,如何便捷的将null值转换为空字符串?
以SpringBoot项目为例,SSM同理。
1、新建配置类(JsonConfig.java)
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.io.IOException;
@Configuration
public class JsonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder)
{
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
// Include.Include.ALWAYS 默认
// Include.NON_DEFAULT 属性为默认值不序列化
// Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
// Include.NON_NULL 属性为NULL 不序列化,就是为null的字段不参加序列化
//objectMapper.setSerializationInclusion(Include.NON_EMPTY);
// 字段保留,将null值转为""
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
{
@Override
public void serialize(Object o, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException, JsonProcessingException
{
jsonGenerator.writeString("");
}
});
return objectMapper;
}
}
2、在启动类Application中,记得添加Scan注解,防止无法扫描到配置类。
ps:下面看下spring boot 使用 json 响应时去除 null 的字段
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
public class RespObject implements Serializable {
private static final long serialVersionUID = -1560603887556641494L;
....
@JsonInclude(Include.NON_NULL)
public Object respMsg;
@JsonInclude(Include.NON_NULL)
public Object respData;
....
}
总结
以上所述是小编给大家介绍的SpringBoot项目中处理返回json的null值,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://www.cnblogs.com/ryantom/archive/2019/10/28/11752616.html
标签:spring,boot,json,null
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java String类的性质与比较
2021-07-29 16:48:37
![](https://img.aspxhome.com/file/2023/2/91732_0s.jpg)
Java中的"goto"语句妙用
2023-10-10 03:54:32
![](https://img.aspxhome.com/file/2023/3/78853_0s.png)
基于C#实现网页爬虫
2021-10-30 08:13:44
![](https://img.aspxhome.com/file/2023/7/72477_0s.jpg)
Hibernate用ThreadLocal模式(线程局部变量模式)管理Session
2021-10-01 18:50:10
Java的super关键字与instanceof运算符使用方法
2022-02-18 10:02:22
详解Java注解知识点
2021-06-24 18:39:12
![](https://img.aspxhome.com/file/2023/5/60905_0s.png)
js 交互在Flutter 中使用 webview_flutter
2023-07-20 22:40:14
![](https://img.aspxhome.com/file/2023/5/91785_0s.jpg)
idea2019导入maven项目中的某些问题及解决方法
2022-10-13 12:30:53
![](https://img.aspxhome.com/file/2023/3/113503_0s.png)
Java中BigDecimal类的add()的使用详解
2023-03-07 16:12:11
聚星C#数字信号处理工具包频谱分析的用法
2022-08-30 11:06:39
![](https://img.aspxhome.com/file/2023/7/69237_0s.png)
详解用Eclipse如何创建Web项目
2023-11-11 05:41:01
![](https://img.aspxhome.com/file/2023/3/59393_0s.png)
Android 多进程资料总结
2021-09-06 14:04:41
Java字符判断的小例子
2023-08-26 17:29:49
![](https://img.aspxhome.com/file/2023/9/58169_0s.jpg)
ZooKeeper入门教程一简介与核心概念
2022-11-24 18:36:00
![](https://img.aspxhome.com/file/2023/9/65749_0s.jpg)
WPF实现列表分页控件的示例代码
2023-12-25 06:46:02
![](https://img.aspxhome.com/file/2023/7/92977_0s.png)
Spring @CrossOrigin 注解原理实现
2022-09-03 10:40:04
![](https://img.aspxhome.com/file/2023/1/71851_0s.png)
java实现单机版五子棋
2022-12-20 20:15:07
![](https://img.aspxhome.com/file/2023/9/63379_0s.jpg)
Mybatis参数传递示例代码
2023-05-20 09:43:08
Android Studio搜索功能(查找功能)及快捷键图文详解
2021-10-23 08:47:20
![](https://img.aspxhome.com/file/2023/9/88119_0s.png)
Android编程基于Contacts读取联系人的方法(附demo源码)
2023-11-22 06:05:39
![](https://img.aspxhome.com/file/2023/5/138165_0s.png)