使用 lombok 自定义序列化和反序列化字段名称
Custom serialized and deserialized field names with lombok
有没有一种方法可以指定不同的 serialized/deserialized JSON 字段名称,而不必显式写出 getter 和 setter 方法,也许使用 lombok getters 和 setters?
与此 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 一起工作。我确实看到 Jackson 支持 @JsonAlias,这在这个特定示例中可能会有所帮助,但我还需要使用不同的名称进行序列化。
看起来应该很简单,大概是这样的:
@Getter
@Setter
public class PrivacySettings {
@JsonSetter("CDO_Name__c")
@JsonGetter("chiefDataOfficerName")
private String chiefDataOfficerName;
}
但这当然是无效的。
对于这种情况,我特别认为 getter 和 setter 没有任何问题。
但是,如果你想尝试,自从 v0.11.8 Lombok 支持 experimental feature to add annotations to generated getters and setters. See the 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 use onParam=@__({@AnnotationsHere})
. Be careful though! This is an experimental feature. For more details see the documentation on the onX feature.
此功能的语法取决于 JDK 版本。对于 JDK 8,您将拥有:
public class PrivacySettings {
@Setter(onMethod_ = { @JsonSetter("CDO_Name__c") })
@Getter(onMethod_ = { @JsonGetter("chiefDataOfficerName") })
private String chiefDataOfficerName;
}
对于JDK7,语法为:
public class PrivacySettings {
@Setter(onMethod = @__({ @JsonSetter("CDO_Name__c") }))
@Getter(onMethod = @__({ @JsonGetter("chiefDataOfficerName") }))
private String chiefDataOfficerName;
}
有关详细信息,请查看 @Getter
and @Setter
documentation. Also see this 了解有关 @__()
的详细信息。
有没有一种方法可以指定不同的 serialized/deserialized JSON 字段名称,而不必显式写出 getter 和 setter 方法,也许使用 lombok getters 和 setters?
与此 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 一起工作。我确实看到 Jackson 支持 @JsonAlias,这在这个特定示例中可能会有所帮助,但我还需要使用不同的名称进行序列化。
看起来应该很简单,大概是这样的:
@Getter
@Setter
public class PrivacySettings {
@JsonSetter("CDO_Name__c")
@JsonGetter("chiefDataOfficerName")
private String chiefDataOfficerName;
}
但这当然是无效的。
对于这种情况,我特别认为 getter 和 setter 没有任何问题。
但是,如果你想尝试,自从 v0.11.8 Lombok 支持 experimental feature to add annotations to generated getters and setters. See the 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.
此功能的语法取决于 JDK 版本。对于 JDK 8,您将拥有:
public class PrivacySettings {
@Setter(onMethod_ = { @JsonSetter("CDO_Name__c") })
@Getter(onMethod_ = { @JsonGetter("chiefDataOfficerName") })
private String chiefDataOfficerName;
}
对于JDK7,语法为:
public class PrivacySettings {
@Setter(onMethod = @__({ @JsonSetter("CDO_Name__c") }))
@Getter(onMethod = @__({ @JsonGetter("chiefDataOfficerName") }))
private String chiefDataOfficerName;
}
有关详细信息,请查看 @Getter
and @Setter
documentation. Also see this @__()
的详细信息。