springboot配置Jackson返回统一默认值的实现示例
作者:阿豪_mike 时间:2023-12-08 13:55:22
在项目开发中,我们返回的数据或者对象没有的时候一般直接返回的null
有数据时的返回值
{
"flag": true,
"code": "10000",
"msg": "成功!",
"data": {
"id": 32,
"templateType": 1,
"templateName": "我的测试模板1",
"freightName": "我的测试标题1",
"listArea": [
{
"id": 968,
"templateId": 32,
"freightPrice": 15,
}
],
"templateDescEntity": {
"id": 1
"name": "xxx"
}
}
}
没有数据时的返回值
{
"flag": true,
"code": "10000",
"msg": "成功!",
"data": {
"id": 32,
"templateType": 1,
"templateName": null,
"freightName": null,
"listArea": null,
"templateDescEntity": null
}
}
这种情况下数据返回给前端,前端需要做大量的空值判断
如前端调使用属性data.templateDescEntity.id的时候就会直接报异常
此时我们可以使用返回值统一处理,配置如下
pom.xml添加
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
java类添加配置
package com.ys.mall.core.product.config;
import com.fasterxml.jackson.core.JsonGenerator;
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;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* 数据返回给前端时,设置null值默认为""
*
* @author cgh
* @date 2020/12/14 10:35
*/
@Configuration
public class JacksonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String fieldName = jsonGenerator.getOutputContext().getCurrentName();
try {
//反射获取字段类型
Field field = jsonGenerator.getCurrentValue().getClass().getDeclaredField(fieldName);
if (CharSequence.class.isAssignableFrom(field.getType())) {
//字符串型空值""
jsonGenerator.writeString("");
return;
} else if (Collection.class.isAssignableFrom(field.getType())) {
//列表型空值返回[]
jsonGenerator.writeStartArray();
jsonGenerator.writeEndArray();
return;
} else if (Map.class.isAssignableFrom(field.getType())) {
//map型空值 或者 bean对象 返回{}
jsonGenerator.writeStartObject();
jsonGenerator.writeEndObject();
return;
}
} catch (NoSuchFieldException ignored) {
}
jsonGenerator.writeString("");
}
});
return objectMapper;
}
}
添加空值统一处理后的返回值
{
"flag": true,
"code": "10000",
"msg": "成功!",
"data": {
"id": 32,
"templateType": 1,
"templateName": "",
"freightName": "",
"listArea": [],
"templateDescEntity": {}
}
}
来源:https://juejin.cn/post/6989848776361902088
标签:springboot,Jackson,返回统一默认值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java反射之通过反射获取一个对象的方法信息(实例代码)
2021-08-07 15:14:46
![](https://img.aspxhome.com/file/2023/2/66532_0s.jpg)
JSON 与对象、集合之间的转换的示例
2021-12-04 20:08:58
如何优雅的进行Spring整合MongoDB详解
2022-10-23 05:24:19
JavaWeb 网上书店 注册和登陆功能案例详解
2022-09-20 02:53:22
![](https://img.aspxhome.com/file/2023/4/92664_0s.png)
android异步消息机制 从源码层面解析(2)
2023-08-06 15:55:39
![](https://img.aspxhome.com/file/2023/2/85972_0s.png)
Android实现截屏并保存操作功能
2021-12-16 19:45:16
Spring Cloud Feign接口返回流的实现
2021-06-07 07:21:54
Java 远程调用失败重试的操作方法
2021-08-26 21:42:58
![](https://img.aspxhome.com/file/2023/7/129987_0s.jpg)
Java swing实现酒店管理系统
2021-08-08 02:24:12
RocketMQ消息过滤与查询的实现
2023-06-26 10:04:25
![](https://img.aspxhome.com/file/2023/0/68700_0s.jpg)
C# Winform实现进度条显示
2023-09-14 15:47:51
![](https://img.aspxhome.com/file/2023/9/68609_0s.png)
Java装饰者模式的示例详解
2022-03-30 07:26:22
![](https://img.aspxhome.com/file/2023/6/114046_0s.jpg)
C#中时间类的使用方法详解
2023-12-17 13:21:08
Java CPU性能分析工具代码实例
2023-09-28 04:52:54
你什么是Elastic Stack(ELK)
2022-12-02 20:12:43
![](https://img.aspxhome.com/file/2023/2/65182_0s.jpg)
java String 转成Double二维数组的方法
2023-04-28 12:11:02
Java Gradle项目中的资源正确获取方式
2022-10-05 09:00:50
![](https://img.aspxhome.com/file/2023/8/70568_0s.jpg)
java使用hadoop实现关联商品统计
2022-11-05 05:55:43
![](https://img.aspxhome.com/file/2023/3/76603_0s.jpg)
Springboot+ElementUi实现评论、回复、点赞功能
2022-06-16 04:34:00
![](https://img.aspxhome.com/file/2023/3/72913_0s.jpg)
深入理解Spring事务原理
2022-01-18 04:16:39
![](https://img.aspxhome.com/file/2023/7/61777_0s.jpg)