JSON 响应中的重复字段
Duplicate fields in JSON Response
我在我的项目中使用 Spring boot Jackson 依赖项和 lombok,作为响应,由于下划线
我得到了重复的字段
这是我的模型class:
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@ToString
public class TcinDpciMapDTO {
@JsonProperty(value = "tcin")
private String tcin;
@JsonProperty(value = "dpci")
private String dpci;
@JsonProperty(value = "is_primary_tcin_in_dpci_relation")
private boolean is_primaryTcin = true;
}
如果我在 is_primaryTcin
字段中使用下划线,我会得到低于重复字段的响应
{
"_primaryTcin": true,
"tcin": "12345",
"dpci": "12345",
"is_primary_tcin_in_dpci_relation": true
}
如果我从字段 isprimaryTcin
中删除下划线,那么我会得到正确的响应
{
"tcin": "12345",
"dpci": "12345",
"is_primary_tcin_in_dpci_relation": true
}
这是因为下划线吗?但下划线更适合用于变量名,对吗?
当您使用未使用下划线的 is_primaryTcin
时,它混合使用了两者。
您可以使用 PropertyNamingStrategy
.
修复它
如果你这样做
...
@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
public class TcinDpciMapDTO {
private String tcin;
private String dpci;
private boolean isPrimaryTcinInDpciRelation = true;
}
JSON 输出将是
{
"tcin": "12345",
"dpci": "12345",
"is_primary_tcin_in_dpci_relation": true
}
这就是你的 class 在 delmboking 之后的样子:
public class TcinDpciMapDTO {
@JsonProperty("tcin")
private String tcin;
@JsonProperty("dpci")
private String dpci;
@JsonProperty("is_primary_tcin_in_dpci_relation")
private boolean is_primaryTcin = true;
public String getTcin() {
return this.tcin;
}
public String getDpci() {
return this.dpci;
}
public boolean is_primaryTcin() {
return this.is_primaryTcin;
}
public TcinDpciMapDTO setTcin(String tcin) {
this.tcin = tcin;
return this;
}
public TcinDpciMapDTO setDpci(String dpci) {
this.dpci = dpci;
return this;
}
public TcinDpciMapDTO set_primaryTcin(boolean is_primaryTcin) {
this.is_primaryTcin = is_primaryTcin;
return this;
}
public TcinDpciMapDTO() {
}
public String toString() {
return "TcinDpciMapDTO(tcin=" + this.getTcin() + ", dpci=" + this.getDpci() + ", is_primaryTcin=" + this.is_primaryTcin() + ")";
}
}
如果未指定生成的 属性 名称,Jackson 通过从 getter 中剥离前缀 is
或 get
来生成它(如果使用 getter 或通过如果在不使用 getter 的情况下序列化字段,则使用 Java 字段名称。默认情况下,Jackson 在序列化期间仅使用 getters。因为你把 @JsonProperty
放在字段上,Jackson 同时使用字段和 getters 并通过匹配生成的 属性 名称检查字段是否已经序列化(无论如何最后一部分是我的猜测)它无法识别从字段 is_primaryTcin
生成的 属性 和从 getter is_primaryTcin()
生成的 属性 相同(一个在内部命名为 is_primaryTcin
,另一个_primaryTcin
) - 请注意,如果将 is_primaryTcin
重命名为 as_primaryTcin
,问题就会消失。
我在我的项目中使用 Spring boot Jackson 依赖项和 lombok,作为响应,由于下划线
我得到了重复的字段这是我的模型class:
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@ToString
public class TcinDpciMapDTO {
@JsonProperty(value = "tcin")
private String tcin;
@JsonProperty(value = "dpci")
private String dpci;
@JsonProperty(value = "is_primary_tcin_in_dpci_relation")
private boolean is_primaryTcin = true;
}
如果我在 is_primaryTcin
字段中使用下划线,我会得到低于重复字段的响应
{
"_primaryTcin": true,
"tcin": "12345",
"dpci": "12345",
"is_primary_tcin_in_dpci_relation": true
}
如果我从字段 isprimaryTcin
中删除下划线,那么我会得到正确的响应
{
"tcin": "12345",
"dpci": "12345",
"is_primary_tcin_in_dpci_relation": true
}
这是因为下划线吗?但下划线更适合用于变量名,对吗?
当您使用未使用下划线的 is_primaryTcin
时,它混合使用了两者。
您可以使用 PropertyNamingStrategy
.
如果你这样做
...
@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
public class TcinDpciMapDTO {
private String tcin;
private String dpci;
private boolean isPrimaryTcinInDpciRelation = true;
}
JSON 输出将是
{
"tcin": "12345",
"dpci": "12345",
"is_primary_tcin_in_dpci_relation": true
}
这就是你的 class 在 delmboking 之后的样子:
public class TcinDpciMapDTO {
@JsonProperty("tcin")
private String tcin;
@JsonProperty("dpci")
private String dpci;
@JsonProperty("is_primary_tcin_in_dpci_relation")
private boolean is_primaryTcin = true;
public String getTcin() {
return this.tcin;
}
public String getDpci() {
return this.dpci;
}
public boolean is_primaryTcin() {
return this.is_primaryTcin;
}
public TcinDpciMapDTO setTcin(String tcin) {
this.tcin = tcin;
return this;
}
public TcinDpciMapDTO setDpci(String dpci) {
this.dpci = dpci;
return this;
}
public TcinDpciMapDTO set_primaryTcin(boolean is_primaryTcin) {
this.is_primaryTcin = is_primaryTcin;
return this;
}
public TcinDpciMapDTO() {
}
public String toString() {
return "TcinDpciMapDTO(tcin=" + this.getTcin() + ", dpci=" + this.getDpci() + ", is_primaryTcin=" + this.is_primaryTcin() + ")";
}
}
如果未指定生成的 属性 名称,Jackson 通过从 getter 中剥离前缀 is
或 get
来生成它(如果使用 getter 或通过如果在不使用 getter 的情况下序列化字段,则使用 Java 字段名称。默认情况下,Jackson 在序列化期间仅使用 getters。因为你把 @JsonProperty
放在字段上,Jackson 同时使用字段和 getters 并通过匹配生成的 属性 名称检查字段是否已经序列化(无论如何最后一部分是我的猜测)它无法识别从字段 is_primaryTcin
生成的 属性 和从 getter is_primaryTcin()
生成的 属性 相同(一个在内部命名为 is_primaryTcin
,另一个_primaryTcin
) - 请注意,如果将 is_primaryTcin
重命名为 as_primaryTcin
,问题就会消失。