簡體   English   中英

如何使用Jackson的ObjectMapper.readerForUpdating忽略某些字段

[英]How to ignore certain fields with Jackson's ObjectMapper.readerForUpdating

我正在使用Jackson 2.7.0

在嘗試使用一些新值更新現有對象時,我試圖忽略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)
  • 在現場和吸氣劑/吸氣劑上:
    • @JsonInclude(Include.NON_EMPTY)
    • @JsonInclude(Include.NON_NULL)
    • @JsonIgnore
    • @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

以上都不是工作! 有幫助嗎?
我想這與readerForUpdating和/或其中一個正在更新的事實有關。

我通過這樣配置ObjectMapper來解決了這個問題(雖然不確定是否全部需要這些):

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

並在Message類中獲取所需的屬性:

@JsonIgnore上的@JsonIgnore (在解析為Java對象時將其排除在外)
@JsonProperty上的@JsonProperty (在解析為JSON對象時包括它)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM