代码之家  ›  专栏  ›  技术社区  ›  Ryuzaki L

json响应中的重复字段

  •  3
  • Ryuzaki L  · 技术社区  · 8 年前

    我在我的项目中使用了SpringBootJackson依赖项和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
    }
    

    这是因为下划线吗?但下划线更适合用在变量名中,对吧?

    2 回复  |  直到 8 年前
        1
  •  4
  •   Konrad Botor    8 年前

    这就是你的课在脱模后的样子:

    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将通过剥离前缀生成它 is get 如果使用getter,则使用getter;如果不使用getter序列化字段,则使用java字段名。默认情况下,jackson只在序列化期间使用getter。因为你把 @JsonProperty 在字段上,jackson同时使用字段和getter,并检查字段是否已经通过匹配生成的属性名进行了序列化(不管怎样,这最后一部分是我的猜测),它不识别从字段生成的属性。 is_primaryTcin 以及从getter生成的属性 is_primaryTcin() 同样的(一个是内部命名的 是第一次 另一个 _primaryTcin )-请注意,如果重命名 是第一次 as_primaryTcin 问题消失了。

        2
  •  2
  •   naturaljoin    8 年前

    当你使用 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
    }