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

东莞企业网站咨询wordpress 注册登录插件

东莞企业网站咨询,wordpress 注册登录插件,discuz视频网站模板,什么是软文一、为什么需要处理空值字段?在实际开发中,我们经常遇到这样的场景:前端需要明确区分字段是null、空字符串还是未提供第三方接口要求严格的数据结构,即使字段为空也必须存在日志记录需要完整的数据结构以便问题排查默认情况下&…

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

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

  • 前端需要明确区分字段是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://wBuOiVwo.ccwzr.cn
http://oDtKuDbM.ccwzr.cn
http://aPQE9jKu.ccwzr.cn
http://l0Bl9INt.ccwzr.cn
http://rxQhqd7F.ccwzr.cn
http://r1xIBNHS.ccwzr.cn
http://Np0bfSVH.ccwzr.cn
http://laSikg0e.ccwzr.cn
http://oEp1CfgZ.ccwzr.cn
http://Ty0r0vQq.ccwzr.cn
http://89WHqVoF.ccwzr.cn
http://1z0KqQ23.ccwzr.cn
http://f4Gft8A7.ccwzr.cn
http://VLq6V8qs.ccwzr.cn
http://RsZWof03.ccwzr.cn
http://i9UhSp8h.ccwzr.cn
http://X1pA6DY0.ccwzr.cn
http://R2JTnynl.ccwzr.cn
http://N0hkPKee.ccwzr.cn
http://IM9vJUgi.ccwzr.cn
http://gkQHMo5G.ccwzr.cn
http://RoH4jeX2.ccwzr.cn
http://uY97OcRd.ccwzr.cn
http://A9xQByNB.ccwzr.cn
http://ESZOIFBz.ccwzr.cn
http://DQL5mSa5.ccwzr.cn
http://rZaT7r84.ccwzr.cn
http://G1JBiQVv.ccwzr.cn
http://cG9rc88V.ccwzr.cn
http://Dbux4J8a.ccwzr.cn
http://www.dtcms.com/wzjs/650266.html

相关文章:

  • 机械加工外协网站长治个人做网站
  • 景区网站建设费用引擎优化seo是什么
  • 青岛网站建设订做网站备案链接代码
  • 网站上线步骤 icp备案眉山市做网站的公司
  • 个人网站免备案吗网络建站公司如何做市场
  • 寻找客户资源的网站手机在线logo免费设计
  • 做足球行业深度内容的网站网站站长登录方式
  • 无棣县建设局网站四川省建设厅申报网站
  • 北辰正方建设集团网站北京网站建设最便宜的公司哪家好
  • 网站内容优化的主要方法wordpress自动推送token
  • 加强人社网站建设360推广开户
  • 自己做鲜花网站怎么样优秀简洁网站设计
  • 北京网站设计制作网站sns营销
  • 洛阳php网站开发高端的咨询行业网站策划
  • 乒乓球网站建设目标济南网页开发公司
  • 如何在手机上开自己的网站北京软件开发学校
  • 网站的轮播图一般是做多大象山经济开发区建设有限公司网站
  • 淘宝客如何建设自己的网站做靓号网站
  • 保险网站定制微信做公司网站怎么做
  • 如何在自己电脑上建设网站百度竞价排名收费标准
  • 创客贴网页设计网站蓬莱网站建设哪家好
  • 做电影网站选择什么配置的服务器配音网站赚钱
  • 小企业网站建设一般收费wordpress pdf下载
  • 中国网站制作 第一个农产品网络营销论文
  • dedecms视频网站开发网站常用模块
  • 有个专门做装修的网站建设企业网站下载
  • 网站建设类型分类怎么建设网站手机网站制作价格
  • 可以把网站建设在云主机上吗做好网站优化的方法有哪些?
  • 深圳建网站服务林管局网站建设方案
  • 网站开发 定制网站ie浏览器不兼容