当前位置: 首页 > ds >正文

问题记录:Fastjson序列化-空值字段处理

一、为什么需要处理空值字段?

在实际开发中,我们经常遇到这样的场景:

  • 前端需要明确区分字段是null、空字符串还是未提供

  • 第三方接口要求严格的数据结构,即使字段为空也必须存在

  • 日志记录需要完整的数据结构以便问题排查

默认情况下,Fastjson会忽略所有值为null的字段,这可能导致:

  1. 前端收到不完整的数据结构

  2. 接口文档与实际响应不一致

  3. 调试时难以区分字段缺失和字段为空

二.解决方案

1.创建消息转化器(适用于接口返回值序列化)

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import java.util.List;@Configuration
public class FastJsonConfiguration implements WebMvcConfigurer {@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {// 1. 创建FastJson消息转换器FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();// 2. 创建FastJson配置FastJsonConfig fastJsonConfig = new FastJsonConfig();// 3. 设置序列化特征:输出空字段fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, // 关键配置:输出值为null的字段SerializerFeature.WriteNullListAsEmpty, // 空List转为[]SerializerFeature.WriteNullStringAsEmpty, // 空String转为""SerializerFeature.WriteDateUseDateFormat, // 日期格式化SerializerFeature.DisableCircularReferenceDetect // 禁用循环引用检测);// 4. 设置日期格式fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");// 5. 将配置设置给转换器fastJsonConverter.setFastJsonConfig(fastJsonConfig);// 6. 将FastJson转换器添加到converters中,并置于首位converters.add(0, fastJsonConverter);}
}

2.字段注解(适用于指定字段)

@JSONField(serialzeFeatures = SerializerFeature.WriteMapNullValue)
public class UserDTO {@JSONField(serialzeFeatures = SerializerFeature.WriteMapNullValue)private String name;@JSONField(serialzeFeatures = SerializerFeature.WriteMapNullValue)private Integer age;@JSONField(serialzeFeatures = SerializerFeature.WriteNullListAsEmpty)private List<String> tags;
}

3.全局配置(适用于项目全局)

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;/*** FastJSON全局配置* 配置全局序列化特性,包括空字段序列化* 这个配置会影响所有使用JSON.toJSONString()的地方*/
@Configuration
public class FastJsonGlobalConfig {private static final Logger logger = LoggerFactory.getLogger(FastJsonGlobalConfig.class);@PostConstructpublic void init() {logger.info("开始配置FastJSON全局序列化特性...");// 使用位运算组合多个特性int features = JSON.DEFAULT_GENERATE_FEATURE;features |= SerializerFeature.WriteMapNullValue.getMask();features |= SerializerFeature.WriteNullListAsEmpty.getMask();features |= SerializerFeature.DisableCircularReferenceDetect.getMask();JSON.DEFAULT_GENERATE_FEATURE = features;logger.info("FastJSON全局序列化特性配置完成");logger.info("启用的特性: {}", SerializerFeature.config(JSON.DEFAULT_GENERATE_FEATURE, new SerializerFeature[0]));}
}

http://www.xdnf.cn/news/15506.html

相关文章:

  • 跨域中间件通俗理解
  • 日记-生活随想
  • LVS负载均衡集群概述
  • C++--List的模拟实现
  • 【时时三省】(C语言基础)通过指针引用数组元素2
  • 20250711_Sudo 靶机复盘
  • 【读书笔记】《Effective Modern C++》第4章 Smart Pointers
  • 串口学习和蓝牙通信HC05(第八天)
  • es里的node和shard是一一对应的关系吗,可以多个shard分配到一个node上吗
  • Pandas-数据清洗与处理
  • 构建可落地的企业AI Agent,背后隐藏着怎样的技术密码?
  • redis汇总笔记
  • 什么时候需要用到 multiprocessing?
  • 基于 CentOS 7 的 LVS+DR+Web+NFS 旅游攻略分享平台部署
  • 【RA-Eco-RA6E2-64PIN-V1.0 开发板】ADC 电压的 LabVIEW 数据采集
  • 【读书笔记】《Effective Modern C++》第六章 Lambda Expressions
  • Windows 常用命令
  • vue防内存泄漏和性能优化浅解
  • 如何自动化处理TXT日志,提升工作效率新方式
  • RabbitMQ队列的选择
  • 03.Python 字符串中的空白字符处理
  • Springboot实现一个接口加密
  • 华为交换机 undo negotiation auto功能(华为交换机端口接光纤两端起不来)
  • 【Complete Search】-基础完全搜索-Basic Complete Search
  • JAVA 项目工程化实践
  • fatal: active `post-checkout` hook found during `git clone`
  • v-for中key值的作用:为什么我总被要求加这个‘没用的‘属性?
  • 大小为 K 且平均值大于等于阈值的子数组数目
  • “找到一个或多个多重定义的符号“(LNK2005 或 LNK1169)
  • 006_测试评估与安全实践