问题描述:
使用fastjson1.2.83作为消息转换器时,定义的response对象中A,B会被序列化为:
{“A”:“testA”,“B”:“testB”}
而升级为fastjson2.0.32后,定义的response对象中A,B会被序列化为:
{“_A”:“testA”,“_B”:“testB”}
原因分析:
fastjson2 默认是基于 public 的 field 和 getter 方法序列化,而上述两个字段 A,B 的 getter 方法被重写为 get_A,get_B。
response对象中的A,B字段:
重现问题的fastjson2源码截图如下:
解决方案:
在mvc的消息转换器中增加一个全局参数(1.2.83默认开启,2为默认关闭):
fastJsonConfig.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.FieldBased);
fastJsonConfig.setReaderFeatures(JSONReader.Feature.SupportSmartMatch, JSONReader.Feature.FieldBased);