alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.
Apache License 2.0
3.74k stars 484 forks source link

fastjson升级成2.025 JSONField 无效 #1762

Open phptalker opened 1 year ago

phptalker commented 1 year ago

问题描述

简要描述您碰到的问题。

fastjson升级成2.025 JSONField 无效

环境信息

请填写以下信息:

重现步骤

如何操作可以重现该问题:

  1. 使用 JSON.toJSONString 方法
  2. 输入 QueryStationInfoParam queryStationInfoParam = new QueryStationInfoParam(); queryStationInfoParam.setPageNo(100); queryStationInfoParam.setPageSize(100); 数据
  3. 出现 序列化结果和预期不一致 错误
bean类:
![Uploading image.png…]()

测试代码
        QueryStationInfoParam queryStationInfoParam = new QueryStationInfoParam();
        queryStationInfoParam.setPageNo(100);
        queryStationInfoParam.setPageSize(100);

        logger.info("params {}", JSON.toJSONString(queryStationInfoParam));

测试结果:hargingServiceImplTest - params {"pageNo":100,"pageSize":100}

期待的正确结果

{"PageNo":100,"PageSize":100}

相关日志输出

{"pageNo":100,"pageSize":100}

附加信息

CodePlayer commented 1 year ago

Fastjson 2 的输出是对的。你的期待才是不符合 JavaBean 规范的。

你应该使用了 @JSONField(name = "PageNo") 来自定义字段名称吧 ?

注意看一下你引用的注解的 包名

你如果是从 Fastjson1 升级上来的,那你的 @JSONField 可能还是 Fastjson 1 的。

使用 Fastjson 2进行序列化,就要使用 Fastjson 2@JSONField 注解。

wenshao commented 1 year ago

QueryStationInfoParam 这个类代码相关的图片看不到,请补充一下