JAVA在序列化时使用@JsonProperty出现字段重复序列化

项目场景:

项目场景:需要将JavaBean序列化为首字母大写传入Redis,供给前端使用WebSocket取值


问题描述

因为需要将前端序列化为大写字母,所以直接在JavaBean属性上直接加了@JsonProperty,但是序列化的时候发现重复字段。

@Data
public class ScadaDO implements Serializable {

    private static final long serialVersionUID = 5703710525986968538L;

    @JsonProperty("PressureSensorFailure")
    private String pressureSensorFailure;

    @JsonProperty("DiagnosticResults")
    private String diagnosticResults;

    @JsonProperty("AMSAlarmDescription")
    private String amsAlarmDescription;

    @JsonProperty("AMSAlarmType")
    private String amsAlarmType;

    @JsonProperty("AMSAlarmTime")
    private String amsAlarmTime;

    @JsonProperty("SetValue")
    private String setValue;

    @JsonProperty("ActualValue")
    private String actualValue;

    @JsonProperty("PSupplyPressureHighAlertPiont")
    private String pSupplyPressureHighAlertPiont;


}

打印:{“diagnosticResults”:null,“psupplyPressureHighAlertPiont”:null,“amsalarmDescription”:null,“pressureSensorFailure”:null,“amsalarmTime”:null,“actualValue”:null,“setValue”:null,“amsalarmType”:null,“PressureSensorFailure”:null,“DiagnosticResults”:null,“AMSAlarmDescription”:null,“AMSAlarmType”:null,“AMSAlarmTime”:null,“SetValue”:null,“ActualValue”:null,“PSupplyPressureHighAlertPiont”:null}


原因分析:

因为在序列化时默认会找get方法,上面加着@Data注解,默认生成了get方法。所以如果没进行更多的配置就相当于序列化了两次


解决方案:

1:去掉get方法
2:加配置如下

@Data
@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE)
public class ScadaDO implements Serializable {

    private static final long serialVersionUID = 5703710525986968538L;

    @JsonProperty("PressureSensorFailure")
    private String pressureSensorFailure;

    @JsonProperty("DiagnosticResults")
    private String diagnosticResults;

    @JsonProperty("AMSAlarmDescription")
    private String amsAlarmDescription;

    @JsonProperty("AMSAlarmType")
    private String amsAlarmType;

    @JsonProperty("AMSAlarmTime")
    private String amsAlarmTime;

    @JsonProperty("SetValue")
    private String setValue;

    @JsonProperty("ActualValue")
    private String actualValue;

    @JsonProperty("PSupplyPressureHighAlertPiont")
    private String pSupplyPressureHighAlertPiont;


}

设置@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE)属性取消 Getxxx方法自动检测

打印如下:
{“PressureSensorFailure”:null,“DiagnosticResults”:null,“AMSAlarmDescription”:null,“AMSAlarmType”:null,“AMSAlarmTime”:null,“SetValue”:null,“ActualValue”:null,“PSupplyPressureHighAlertPiont”:null}

这就ok了

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值