【问题标题】:Jackson JSON java class - fields are serialized multiple timesJackson JSON java类 - 字段被序列化多次
【发布时间】:2011-09-26 15:25:11
【问题描述】:

我定义了以下类

@JsonTypeName("PhotoSetUpdater")
public class PhotoSetUpdater {
@JsonProperty("Title")
private String title;
@JsonProperty("Caption")
private String caption;
@JsonProperty("Keywords")
private String[] keywords;
@JsonProperty("Categories")
private int[] categories;
@JsonProperty("CustomReference")
    private String customReference;      // new in version 1.1


public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getCaption() {
    return caption;
}

public void setCaption(String caption) {
    this.caption = caption;
}


public String getCustomReference() {
    return customReference;
}


public void setCustomReference(String customReference) {
    this.customReference = customReference;
}


public void setKeywords(String[] keywords) {
    this.keywords = keywords;
}

public String[] getKeywords() {
    return keywords;
}


public void setCategories(int[] categories) {
    this.categories = categories;
}


public int[] getCategories() {
    return categories;
}

}

问题是,在我用 Jackson JSON 序列化器序列化这个类之后,这些字段在结果负载中插入了两次:一个以小写字母开头,一个以大写字母开头:

... {"Caption":"aa","caption":"aa",...}

类型定义可能有什么问题?

问候

【问题讨论】:

    标签: java android json jackson resttemplate


    【解决方案1】:

    尝试在课堂上使用@JsonAutoDetect(getterVisibility=Visibility.NONE)

    【讨论】:

    • 这行得通——或者你可以在getter上添加@JsonProperty;尽管在 1.8 及更早版本中这会导致重复(需要同时在 getter 和 setter 上)。 Jackson 1.9 将(最终!)合并注释,因此将其添加到 getter 对 getter 和 setter 都有效。
    猜你喜欢
    • 2012-07-04
    • 1970-01-01
    • 2023-03-04
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 2020-07-07
    • 2019-11-25
    相关资源
    最近更新 更多