Dea*_*ool 4 java jackson lombok spring-boot
我在我的项目中使用 Spring boot Jackson 依赖项和 lombok,作为响应,由于下划线,我得到了重复的字段
这是我的模型类:
@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;
}
Run Code Online (Sandbox Code Playgroud)
如果我在is_primaryTcin字段中使用下划线,我会收到以下带有重复字段的响应
{
"_primaryTcin": true,
"tcin": "12345",
"dpci": "12345",
"is_primary_tcin_in_dpci_relation": true
}
Run Code Online (Sandbox Code Playgroud)
如果我从字段中删除下划线,isprimaryTcin那么我会得到正确的响应
{
"tcin": "12345",
"dpci": "12345",
"is_primary_tcin_in_dpci_relation": true
}
Run Code Online (Sandbox Code Playgroud)
这是因为下划线吗?但下划线更喜欢在变量名中使用,对吧?
这是您的班级在 deomboking 后的样子:
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() + ")";
}
}
Run Code Online (Sandbox Code Playgroud)
如果未指定生成的属性名称,Jackson 将通过剥离前缀is或get从 getter(如果使用 getter)来生成它,或者通过使用 Java 字段名称(如果不使用 getter 序列化字段)来生成它。默认情况下 Jackson 仅在序列化期间使用 getter。因为您放置@JsonProperty了字段,Jackson 同时使用字段和 getter,并通过匹配生成的属性名称来检查该字段是否已经序列化(最后一部分是我的猜测),它不会将字段生成的属性is_primaryTcin和 getter 生成的属性识别is_primaryTcin()为相同(一个是内部命名的is_primaryTcin,另一个是内部命名的_primaryTcin) - 请注意,如果您重命名is_primaryTcin为as_primaryTcin问题就会消失。
| 归档时间: |
|
| 查看次数: |
3152 次 |
| 最近记录: |