3

I'm using Jackson 2.7.0

I'm trying to ignore encodingType when updating an existing object with some new values:

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

message contains a value for encodingType.
messageSubset (JSON-string) does not contain an entry (no key-value) for encodingType.

What I've tried:

  • For the ObjectMapper:
    • om.setSerializationInclusion(Include.NON_EMPTY);
  • On the message class:
    • @JsonIgnoreProperties(ignoreUnknown = true)
    • @JsonIgnoreProperties(value = { "encodingType" })
    • @JsonInclude(Include.NON_EMPTY)
    • @JsonInclude(Include.NON_NULL)
  • On the field and on the getters/setters:
    • @JsonInclude(Include.NON_EMPTY)
    • @JsonInclude(Include.NON_NULL)
    • @JsonIgnore
    • @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

Non of the above work! Any help?
I suppose this has something to do with readerForUpdating and/or the fact that one of them is being updated.

4

1 回答 1

1

我通过像这样配置 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在 setter 上(解析为 Java 对象时排除它)
@JsonProperty在 getter 上(在解析为 JSON 对象时包含它)

于 2016-03-08T10:27:15.967 回答