- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有一种方法可以指定不同的序列化/反序列化 JSON 字段名称,而不必显式写出 getter 和 setter 方法,也许使用 lombok getter 和 setter?
与此类似 example ,以下代码允许将传入的 JSON 反序列化为不同的 POJO 字段名称。它还导致 POJO 字段名称按原样序列化:
public class PrivacySettings {
private String chiefDataOfficerName;
@JsonProperty("CDO_Name__c")
private void setChiefDataOfficerName(String chiefDataOfficerName) {
this.chiefDataOfficerName = chiefDataOfficerName;
}
@JsonProperty("chiefDataOfficerName")
private String getChiefDataOfficerName() {
return chiefDataOfficerName;
}
}
@Getter
@Setter
public class PrivacySettings {
@JsonSetter("CDO_Name__c")
@JsonGetter("chiefDataOfficerName")
private String chiefDataOfficerName;
}
最佳答案
在这种情况下,我特别认为 getter 和 setter 没有任何问题。
但是,如果您想尝试,由于 v0.11.8 Lombok 支持 experimental feature为生成的 getter 和 setter 添加注释。见 documentation :
To put annotations on the generated method, you can use
onMethod=@__({@AnnotationsHere})
; to put annotations on the only parameter of a generated setter method, you can useonParam=@__({@AnnotationsHere})
. Be careful though! This is an experimental feature. For more details see the documentation on the onX feature.
public class PrivacySettings {
@Setter(onMethod_ = { @JsonSetter("CDO_Name__c") })
@Getter(onMethod_ = { @JsonGetter("chiefDataOfficerName") })
private String chiefDataOfficerName;
}
public class PrivacySettings {
@Setter(onMethod = @__({ @JsonSetter("CDO_Name__c") }))
@Getter(onMethod = @__({ @JsonGetter("chiefDataOfficerName") }))
private String chiefDataOfficerName;
}
@Getter
和
@Setter
文档。另见此
answer详情
@__()
.
关于json - 使用 lombok 自定义序列化和反序列化字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49886553/
概述 CentOS Stream 成立于 2019 年,是“RHEL 下一步的滚动预览”。Red Hat 首席技术官 Chris Wright 和 CentOS 社区经理 Rich Bowen 各
我有一个使用 Mesosphere DC/OS 编排选项进行配置的 Azure 容器服务 (ACS) 集群。我可以在 Marathon UI 中创建一个应用程序。 但是,当我通过 Marathon U
我是一名优秀的程序员,十分优秀!