问题记录:Fastjson序列化-空值字段处理
一、为什么需要处理空值字段?
在实际开发中,我们经常遇到这样的场景:
前端需要明确区分字段是
null
、空字符串还是未提供第三方接口要求严格的数据结构,即使字段为空也必须存在
日志记录需要完整的数据结构以便问题排查
默认情况下,Fastjson会忽略所有值为null
的字段,这可能导致:
前端收到不完整的数据结构
接口文档与实际响应不一致
调试时难以区分字段缺失和字段为空
二.解决方案
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]));}
}