2016-03-02 125 views
0

我使用的是傑克遜2.7.0如何忽略某些領域與傑克遜的ObjectMapper.readerForUpdating

我試圖忽視encodingType更新了一些新值的現有對象時:

ObjectMapper om = new ObjectMapper(); 
om.readerForUpdating(message).readValue(messageSubset); 

message包含值爲encodingType
messageSubset(JSON字符串)不包含encodingType的條目(無鍵值)。

我已經試過:

  • 對於ObjectMapper:
    • om.setSerializationInclusion(Include.NON_EMPTY);
  • 在消息類:
    • @JsonIgnoreProperties(ignoreUnknown = true)
    • @JsonIgnoreProperties(value = { "encodingType" })
    • @JsonInclude(Include.NON_EMPTY)
    • @JsonInclude(Include.NON_NULL)
  • 在現場和getter/setter方法:
    • @JsonInclude(Include.NON_EMPTY)
    • @JsonInclude(Include.NON_NULL)
    • @JsonIgnore
    • @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

非上述工作!任何幫助?
我想這與readerForUpdating和/或其中一個正在更新的事實有關。

+0

我剛剛試過你的代碼與虛擬'消息'類其中'encodingType'是一個'字符串',它工作正常。不需要註釋。你可以發佈'message'類和一個JSON例子的代碼嗎? –

+0

我通過像下面這樣通過配置ObjectMapper解決了這個問題: 'om.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);' 'om.configure(DeserializationFeature。FAIL_ON_UNKNOWN_PROPERTIES,FALSE);' 'om.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,FALSE);' 而上的消息類爲正確的屬性: '@ JsonIgnore'上的setter '@ JsonProperty'在吸氣劑上 –

回答

0

我通過配置ObjectMapper這樣解決了這一問題(不知道如果這些都不過需要):

om.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); om.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);

而且對所需要的屬性信息類:

@JsonIgnore關於setter(解析到Java對象時不包括它)
@JsonProperty關於getter(include當它解析到JSON對象)