FastJSON 默认行为:JSON.toJSONString 忽略 null 字段

ops/2025/2/27 9:48:46/

完整的 FakeRegistrationController 代码,这让我可以全面分析后端逻辑,特别是为什么空的字段(如 compareDate)不返回给前端。我将详细分析代码的每个接口,尤其是与 list 请求和字段返回相关的部分,并解释原因。


代码结构总览

FakeRegistrationController 是一个 RESTful 控制器,提供了三个接口:

  1. POST /fakeRegistration/registration/save:保存或修改假货登记信息。
  2. POST /fakeRegistration/compare/save:保存或修改比对结果。
  3. GET /fakeRegistration/list:获取假货登记列表(对应前端的 qlist 请求)。

我们关心的是 list 接口,因为它直接决定了前端表格接收到的数据内容,特别是为何空的 compareDate 不返回。


详细分析 list 接口

接口定义
@GetMapping("list")
@ApiOperation("假货登记列表")
public BaseResult list(@ApiIgnore @SessionAttribute(Constants.ADMIN_ID) Integer adminId,BasePage page) {Integer vipAdminId = copywritingApiService.getVipAdminId(adminId, 11);Page<FakeRegistration> fakeRegistrationPage = fakeRegistrationService.findByAdminId(vipAdminId, page);List<FakeRegistration> fakeRegistrations = fakeRegistrationPage.getContent();List<FakeRegistrationListDTO> fakeRegistrationListDTOS = new ArrayList<>();for (FakeRegistration fakeRegistration: fakeRegistrations) {FakeRegistrationListDTO fakeRegistrationListDTO = JSON.parseObject(JSON.toJSONString(fakeRegistration), FakeRegistrationListDTO.class);if (null != fakeRegistration.getProductId()) {Product product = productService.findById(fakeRegistration.getProductId()).orElseThrow(() -> new RuntimeException("未找到商品信息"));fakeRegistrationListDTO.setProductName(product.getName());}Admin creator = adminService.findById(fakeRegistration.getCreatorId()).orElseThrow(() -> new RuntimeException("未找到创建人信息"));fakeRegistrationListDTO.setCreatorName(StringUtils.isEmpty(creator.getNickname()) ? creator.getUsername() : creator.getNickname());fakeRegistrationListDTO.setGenuineIdentificationPoints(fakeRegistrationApiService.findIdentification(fakeRegistration.getId(), 1));fakeRegistrationListDTO.setFakeIdentificationPoints(fakeRegistrationApiService.findIdentification(fakeRegistration.getId(), 0));fakeRegistrationListDTOS.add(fakeRegistrationListDTO);}return BaseResult.success(new PageImpl<>(fakeRegistrationListDTOS, PageRequest.of(fakeRegistrationPage.getNumber(), fakeRegistrationPage.getSize()), fakeRegistrationPage.getTotalElements()));
}
数据流分析
  1. 数据查询

    • fakeRegistrationService.findByAdminId(vipAdminId, page) 返回一个 Page<FakeRegistration>,其中 FakeRegistration 是数据库实体类,包含所有字段(如 id, createdDate, compareDate, comparisonStatus 等)。
    • fakeRegistrations 是分页内容的 List<FakeRegistration>
  2. 数据转换

    • 每个 FakeRegistration 被转换为 FakeRegistrationListDTO
      FakeRegistrationListDTO fakeRegistrationListDTO = JSON.parseObject(JSON.toJSONString(fakeRegistration), FakeRegistrationListDTO.class);
      
    • 这里使用了 FastJSON(com.alibaba.fastjson.JSON)进行序列化和反序列化:
      • JSON.toJSONString(fakeRegistration)FakeRegistration 转为 JSON 字符串。
      • JSON.parseObject(..., FakeRegistrationListDTO.class) 将 JSON 字符串转为 FakeRegistrationListDTO 对象。
    • 然后手动补充了:
      • productName:从 ProductService 获取。
      • creatorName:从 AdminService 获取。
      • genuineIdentificationPointsfakeIdentificationPoints:从 FakeRegistrationApiService 获取。
  3. 返回结果

    • 返回一个 PageImpl<FakeRegistrationListDTO>,最终被序列化为 JSON 响应。
为什么空的字段(如 compareDate)不返回?
  • FastJSON 的默认行为

    • FastJSON 在序列化时,默认忽略 null 值字段,除非显式配置 SerializerFeature.WriteMapNullValue
    • 如果 FakeRegistrationcompareDatenullJSON.toJSONString(fakeRegistration) 生成的 JSON 字符串不会包含 "compareDate"
    • 示例:
      FakeRegistration fr = new FakeRegistration();
      fr.setId(34);
      fr.setComparisonStatus(1);
      fr.setCompareDate(null); // 为空
      String json = JSON.toJSONString(fr);
      // 输出: {"id":34,"comparisonStatus":1}
      
    • 反序列化到 FakeRegistrationListDTO 时,由于 JSON 中没有 compareDatefakeRegistrationListDTOcompareDate 字段不会被赋值,最终返回的 JSON 也不会包含它。
  • 业务逻辑

    • 从其他接口看,compareDate 只有在 compare/save 接口保存比对结果时才会设置:
      fakeRegistrationOrigin.setCompareDate(new Date());
      
    • registration/save 接口中,新建记录时没有设置 compareDate,它保持为 null
    • 因此,未完成比对的记录(comparisonStatus = 01)在数据库中 compareDate 就是 NULL,序列化后被忽略。
  • DTO 定义的影响

    • 如果 FakeRegistrationListDTO 中定义了 compareDate
      public class FakeRegistrationListDTO {private Integer id;private Date compareDate; // 假设是这样// 其他字段
      }
      
    • JSON.parseObject 处理没有 compareDate 的 JSON 时,fakeRegistrationListDTO.compareDate 会是 null,但后续的序列化(返回给前端时)仍由 FastJSON 处理,又会被忽略。

其他接口的补充分析

1. registration/save
@PostMapping("registration/save")
public BaseResult save(@RequestBody FakeRegistration fakeRegistration, ...) {if(null != fakeRegistration.getId()) {FakeRegistration fakeRegistrationOrigin = fakeRegistrationService.findById(fakeRegistration.getId()).orElseThrow(...);fakeRegistration = SqlUtil.mergeObject(fakeRegistration, fakeRegistrationOrigin);if(null != fakeRegistration.getCompareResult() && fakeRegistration.getCompareResult() == 1) {fakeRegistrationOrigin.setComparisonStatus(3);} else if(null != fakeRegistration.getCompareResult() && fakeRegistration.getCompareResult() == 0) {fakeRegistrationOrigin.setComparisonStatus(2);}} else {fakeRegistration.setAdminId(vipAdminId);fakeRegistration.setCreatorId(adminId);}fakeRegistration = fakeRegistrationService.save(fakeRegistration);...
}
  • 关键点
    • 新建记录时(fakeRegistration.getId() == null),只设置了 adminIdcreatorId,没有初始化 compareDate,它默认为 null
    • 修改记录时,如果提供了 compareResult,会更新 comparisonStatus,但不会设置 compareDate
    • 因此,compareDate 在这个接口中始终不会被赋值。
2. compare/save
@PostMapping("compare/save")
public BaseResult compareSave(@RequestBody FakeRegistrationSaveDTO fakeRegistration, ...) {FakeRegistration fakeRegistrationOrigin = fakeRegistrationService.findById(fakeRegistration.getId()).orElseThrow(...);fakeRegistrationOrigin.setCompareResult(fakeRegistration.getCompareResult());fakeRegistrationOrigin.setCompareDate(new Date());fakeRegistrationService.save(fakeRegistrationOrigin);...
}
  • 关键点
    • 这里显式设置了 compareDate = new Date(),表示比对完成的时间。
    • 只有通过这个接口保存比对结果时,compareDate 才会被赋值。
    • 这解释了前端数据中,只有 comparisonStatus = 2 或更高状态的记录有 compareDate

综合结论:为什么空的字段不返回?

  1. 数据来源

    • compareDate 在数据库中默认为 NULL,除非通过 compare/save 接口保存比对结果时才会被赋值。
    • 未完成比对的记录(comparisonStatus = 01)没有 compareDate 值。
  2. FastJSON 序列化

    • list 接口中,JSON.toJSONString(fakeRegistration)FakeRegistration 转为 JSON 时,默认忽略 null 字段。
    • 如果 compareDatenull,它不会出现在 JSON 字符串中。
    • 反序列化到 FakeRegistrationListDTO 后,compareDate 仍未被赋值,最终返回的 JSON 也不包含这个字段。
  3. 业务逻辑

    • 设计上,compareDate 只有在比对完成时才有意义,未完成时保持 null 并被忽略是合理的。
  4. 最终返回

    • Spring Boot 的 @RestController 使用 Jackson(或其他配置)将 PageImpl<FakeRegistrationListDTO> 序列化为 JSON,同样忽略 null 字段,进一步确保空的 compareDate 不出现在响应中。

前端数据的验证

你的前端数据:

  • id: 34, 33, 32comparisonStatus = 01):没有 compareDate,因为未调用 compare/save
  • id: 31, 30, 28comparisonStatus = 2):有 compareDate,因为通过 compare/save 设置了值。

改进建议

如果希望空的 compareDate 也返回(例如 "compareDate": null):

  1. 修改 FastJSON 配置

    FakeRegistrationListDTO fakeRegistrationListDTO = JSON.parseObject(JSON.toJSONString(fakeRegistration, SerializerFeature.WriteMapNullValue),FakeRegistrationListDTO.class
    );
    
    • 添加 WriteMapNullValue,确保 null 字段被包含。
  2. 调整 DTO

    • FakeRegistrationListDTO 中显式处理:
      fakeRegistrationListDTO.setCompareDate(fakeRegistration.getCompareDate() != null ? fakeRegistration.getCompareDate() : null);
      
  3. 全局配置

    • 在 Spring Boot 中配置 Jackson:
      @Bean
      public ObjectMapper objectMapper() {ObjectMapper mapper = new ObjectMapper();mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);return mapper;
      }
      

总结

空的 compareDate 不返回给前端是因为:

  • 业务逻辑:未完成比对时,数据库中 compareDateNULL
  • FastJSON 默认行为JSON.toJSONString 忽略 null 字段。
  • 设计选择:后端选择不返回无意义的空字段。

这种行为是合理的,但如果前端需要一致性,可以通过上述方式调整后端返回。

在这里插入图片描述


http://www.ppmy.cn/ops/161645.html

相关文章

Python Pandas带多组参数和标签的Oracle数据库批量数据导出程序

设计一个基于多个带标签的PL/SQL模板作为配置文件和多组参数的Python Pandas代码程序&#xff0c;实现根据不同的输入参数自动批量地将Oracle数据库中的数据导出为CSV和Excel文件到指定目录上&#xff0c;标签和多个参数&#xff08;以“_”分割&#xff09;为组成导出数据文件…

Java线程池实战:如何避免常见坑点并优化性能?

在使用Java线程池时&#xff0c;避免常见坑点并优化性能是非常重要的。以下是一些关键的实践和建议&#xff0c;帮助你更好地管理和优化线程池的性能。 1. 选择合适的线程池类型 Java提供了几种不同类型的线程池&#xff0c;每种适用于不同的场景&#xff1a; FixedThreadPoo…

形式化数学编程在AI医疗中的探索路径分析

一、引言 1.1 研究背景与意义 在数字化时代,形式化数学编程和 AI 形式化医疗作为前沿领域,正逐渐改变着我们的生活和医疗模式。形式化数学编程是一种运用数学逻辑和严格的形式化语言来描述和验证程序的技术,它通过数学的精确性和逻辑性,确保程序的正确性和可靠性。在软件…

插件化事件处理

以下是一个完整的、可扩展的C++事件处理插件实现,基于先前的功能需求和优化建议。该插件能够灵活地处理不同类型的事件,并支持动态插件注册和卸载。 事件处理插件完整代码 #include <iostream> #include <string> #include <vector>

快手弹幕 websocket 分析

声明: 本文章中所有内容仅供学习交流使用&#xff0c;不用于其他任何目的&#xff0c;抓包内容、敏感网址、数据接口等均已做脱敏处理&#xff0c;严禁用于商业用途和非法用途&#xff0c;否则由此产生的一切后果均与作者无关&#xff01; 逆向分析 import timeimport requests…

Android 布局系列(二):FrameLayout 布局的应用

引言 在安卓开发中&#xff0c;布局管理是构建用户界面的核心之一。对于简单的界面或是需要叠加多个视图的场景&#xff0c;FrameLayout 是一个非常实用的布局容器。它是安卓中最基础的布局之一&#xff0c;能够帮助我们轻松管理多个视图的叠加。尽管它没有复杂的排版功能&…

图数据库Neo4j面试内容整理-使用场景-社交网络

社交网络 是图数据库应用的典型场景之一,因为社交网络本身就具有图结构的特点:人是节点,朋友、关注关系等是关系,而这些节点和关系经常会具有不同的属性。图数据库(如 Neo4j)非常适合存储和查询这些复杂的图数据,可以高效地解决许多社交网络中的查询需求。 1. 社交网络中…

HTML——前端基础1

目录 前端概述 前端能做的事情​编辑 两步完成一个网页程序 前端工具的选择与安装 HTML HTML5介绍 HTML5的DOCTYPE声明 HTML基本骨架 文字标签 标题之标签 标签之段落、换行、水平线 标签之图片 标签之超文本链接 标签之文本 列表标签之有序列表 列表标签之无序…