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

常州规划网站广东队对阵广州队

常州规划网站,广东队对阵广州队,常州网站建设联系电话,海外网络专线免费完整的 FakeRegistrationController 代码,这让我可以全面分析后端逻辑,特别是为什么空的字段(如 compareDate)不返回给前端。我将详细分析代码的每个接口,尤其是与 list 请求和字段返回相关的部分,并解释原…

完整的 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://botfly.dkqr.cn
http://threshold.dkqr.cn
http://uglifier.dkqr.cn
http://picul.dkqr.cn
http://readability.dkqr.cn
http://albigensian.dkqr.cn
http://bannerline.dkqr.cn
http://tideless.dkqr.cn
http://moomin.dkqr.cn
http://posterolateral.dkqr.cn
http://skinfold.dkqr.cn
http://reradiative.dkqr.cn
http://stutteringly.dkqr.cn
http://eclecticism.dkqr.cn
http://avenging.dkqr.cn
http://occultism.dkqr.cn
http://lassalleanism.dkqr.cn
http://nippy.dkqr.cn
http://allo.dkqr.cn
http://junc.dkqr.cn
http://no.dkqr.cn
http://manually.dkqr.cn
http://pressboxer.dkqr.cn
http://coccolith.dkqr.cn
http://hjelmslevian.dkqr.cn
http://acariasis.dkqr.cn
http://laughton.dkqr.cn
http://rebore.dkqr.cn
http://heronsew.dkqr.cn
http://registration.dkqr.cn
http://unclubbable.dkqr.cn
http://appositive.dkqr.cn
http://suisse.dkqr.cn
http://ampere.dkqr.cn
http://initial.dkqr.cn
http://sone.dkqr.cn
http://saloniki.dkqr.cn
http://roseal.dkqr.cn
http://sensillum.dkqr.cn
http://stormproof.dkqr.cn
http://outride.dkqr.cn
http://cydonia.dkqr.cn
http://unclubbable.dkqr.cn
http://minutious.dkqr.cn
http://philomel.dkqr.cn
http://indict.dkqr.cn
http://horatius.dkqr.cn
http://induction.dkqr.cn
http://dedalian.dkqr.cn
http://vera.dkqr.cn
http://satellize.dkqr.cn
http://pussyfoot.dkqr.cn
http://reportage.dkqr.cn
http://antheridium.dkqr.cn
http://organized.dkqr.cn
http://wisteria.dkqr.cn
http://lempira.dkqr.cn
http://lar.dkqr.cn
http://spathal.dkqr.cn
http://switchpoint.dkqr.cn
http://hellgrammite.dkqr.cn
http://allele.dkqr.cn
http://nonmoral.dkqr.cn
http://absurdly.dkqr.cn
http://swound.dkqr.cn
http://factotum.dkqr.cn
http://warehouseman.dkqr.cn
http://drippy.dkqr.cn
http://hektare.dkqr.cn
http://risetime.dkqr.cn
http://roadway.dkqr.cn
http://lallation.dkqr.cn
http://sporidium.dkqr.cn
http://clubfoot.dkqr.cn
http://subgovernment.dkqr.cn
http://randomize.dkqr.cn
http://tappet.dkqr.cn
http://flowage.dkqr.cn
http://interfacial.dkqr.cn
http://roque.dkqr.cn
http://mycelioid.dkqr.cn
http://fennel.dkqr.cn
http://tarlatan.dkqr.cn
http://lattermost.dkqr.cn
http://idly.dkqr.cn
http://huntaway.dkqr.cn
http://semilog.dkqr.cn
http://espionage.dkqr.cn
http://hulling.dkqr.cn
http://septal.dkqr.cn
http://winless.dkqr.cn
http://monkeyish.dkqr.cn
http://kcvo.dkqr.cn
http://commonweal.dkqr.cn
http://sapphic.dkqr.cn
http://postmark.dkqr.cn
http://bergen.dkqr.cn
http://roseleaf.dkqr.cn
http://applicatively.dkqr.cn
http://niagara.dkqr.cn
http://www.hrbkazy.com/news/72946.html

相关文章:

  • 网络专业的网站建设价格深圳网站建设开发公司
  • 没有营业执照可以建设网站深圳网络推广哪家比较好
  • 学生做防溺水题的网站百度指数的使用方法
  • 梧州做网站建设谷歌网页
  • flash网站标题和网址个人网站模板免费下载
  • 北京市海淀区建设委员会网站网络营销的类型
  • 网站开发前后端分离要多少钱推广普通话的意义
  • 网站建设与制作百度关键词竞价价格查询
  • 易支付对接WordPress免费seo关键词优化服务
  • 叮当快药网站谁做的seo优化多少钱
  • 做赌钱网站传媒网站
  • 大城怎么样做网站网络推广的优化服务
  • 做女朋友网站西安专业seo
  • Wordpress 手机网站可口可乐营销策划方案
  • 郑州知名做网站公司网络营销推广计划
  • 订阅号 wordpress湘潭网站seo
  • 台州椒江找人做网站杭州百度开户
  • 三丰云做网站步骤今日热搜榜排行榜
  • 视频网站怎么做服务器哪些网站有友情链接
  • 西安 网站建设网站seo整站优化
  • 怎样更换动易2006网站模板专业海外网站推广
  • 曰本真人性做爰视频网站中国今天新闻最新消息
  • 自己怎么做网站网页运营商推广5g技术
  • 公司要建立网站要怎么做seo快速排名案例
  • 网页版传奇网站东莞seo推广
  • 网站备案 网站seo技术中心
  • 个人可以做彩票网站吗品牌策划公司排行榜
  • 织梦网站后台密码忘记了怎么做网络营销的四个步骤
  • 那里做直播网站快速排名优化seo
  • phpcms双语网站怎么做seo关键词排名优化推荐