我只能想到一個非傑克遜的解決方案,要使用不具有映射參考,然後轉換爲實際的類的基類:
// expect a B on an incoming request
class B {
// ...
}
// after the data is read, cast to A which will have empty references
class A extends B {
public Map<String,List<String>> references;
}
爲什麼如果你不想要它們,你甚至會發送參考文獻?
或者傳入的數據不在您的手中,您只是想避免映射異常,告訴您傑克遜無法找到用於設置傳入引用的屬性?爲此,我們使用一個基類,所有我們的Json模型類的繼承:
public abstract class JsonObject {
@JsonAnySetter
public void handleUnknown(String key, Object value) {
// for us we log an error if we can't map but you can skip that
Log log = LogFactory.getLog(String.class);
log.error("Error mapping object of type: " + this.getClass().getName());
log.error("Could not map key: \"" + key + "\" and value: \"" + "\"" + value.toString() + "\"");
}
然後在POJO添加@JsonIgnoreProperties
,使引入屬性將被轉發到handleUnknown()
@JsonIgnoreProperties
class A extends JsonObject {
// no references if you don't need them
}
編輯
This SO Thread描述瞭如何使用Mixins。這可能是解決方案,如果你想保持你的結構完全一樣,但我沒有嘗試過。
這是正確的。傳入的數據並不在我們手中。但客戶端總是以正確的格式發送引用屬性,所以handleUnknown永遠不會被調用。 – guerilla
啊,我的不好,忘了一點信息。如果您想忽略傳入數據,您需要在POJO上使用'@ JsonIgnoreProperties'。我會更新我的帖子。 (PS:我們使用它來消費Facebook的REST資源,並且從Facebook發送的所有不包含在我們的POJO中的屬性將被記錄下來,然後我們決定是否要添加它們) – Pete
但是引用在我的POJO中總是與制定者匹配 – guerilla