[英]How to ignore certain fields with Jackson's ObjectMapper.readerForUpdating
I'm using Jackson 2.7.0 我正在使用Jackson 2.7.0
I'm trying to ignore encodingType
when updating an existing object with some new values: 在尝试使用一些新值更新现有对象时,我试图忽略
encodingType
:
ObjectMapper om = new ObjectMapper();
om.readerForUpdating(message).readValue(messageSubset);
message
contains a value for encodingType
. message
包含encodingType
的值。
messageSubset
(JSON-string) does not contain an entry (no key-value) for encodingType
. messageSubset
(JSON字符串)不包含encodingType
的条目(无键值)。
What I've tried: 我尝试过的:
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)
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. 我想这与readerForUpdating和/或其中一个正在更新的事实有关。
I fixed the problem by configuring the ObjectMapper like this (not sure if these are all needed though): 我通过这样配置ObjectMapper来解决了这个问题(虽然不确定是否全部需要这些):
om.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
om.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
And on the Message class for the properties needed: 并在Message类中获取所需的属性:
@JsonIgnore
on the setter (excludes it when parsing to the Java object) @JsonIgnore
上的@JsonIgnore
(在解析为Java对象时将其排除在外)
@JsonProperty
on the getter (includes it when parsing to the JSON object) @JsonProperty
上的@JsonProperty
(在解析为JSON对象时包括它)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.