【问题标题】:Duplicate fields in JSON ResponseJSON 响应中的重复字段
【发布时间】:2018-12-21 04:08:13
【问题描述】:

我在我的项目中使用 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;

 }

如果我在 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
}

这是因为下划线吗?但是下划线更喜欢用在变量名中对吧?

【问题讨论】:

  • 不,在 java 中,变量名使用小写形式。

标签: java spring-boot jackson lombok


【解决方案1】:

当您使用不使用下划线的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
}

【讨论】:

    【解决方案2】:

    这是你的班级在 delomboking 后的样子:

    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 中剥离前缀 isget 来生成它,如果使用 getter,或者如果序列化字段而不使用 getter,则使用 Java 字段名称。默认情况下,Jackson 仅在序列化期间使用 getter。因为您将@JsonProperty 放在字段上,所以杰克逊同时使用字段和getter,并通过匹配生成的属性名称检查该字段是否已经序列化(这最后一部分是我的猜测)它无法识别从字段is_primaryTcin 生成的属性和从 getter is_primaryTcin() 生成的属性相同(一个在内部命名为 is_primaryTcin,另一个在内部命名为 _primaryTcin) - 请注意,如果将 is_primaryTcin 重命名为 as_primaryTcin,问题就会消失。

    【讨论】:

      猜你喜欢
      • 2016-08-30
      • 2019-12-20
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多