简体   繁体   English

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

[英]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: 我尝试过的:

  • For the ObjectMapper: 对于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. 我想这与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.

相关问题 Jackson ObjectMapper:如何从序列化中省略(忽略)某些类型的字段? - Jackson ObjectMapper: How to omit (ignore) fields of certain type from serialization? 如何使用 readerForUpdating().readValue 方法强制 Jackson 对象映射器忽略不完整的字段 - How to force Jackson object mapper to ignore not full fields with readerForUpdating().readValue method 如何使用 Jackson 的 objectMapper 反序列化接口字段? - How to deserialize interface fields using Jackson's objectMapper? 使用杰克逊(ObjectMapper)如何将对象序列化为json并忽略除我注释为@JsonProperty的字段以外的所有其他字段? - Using Jackson (ObjectMapper) how serialize object to json and ignore all fields except the ones I annotate as @JsonProperty? 如何将Jackson ObjectMapper配置为使用自定义值序列化某些bean的某些字段(如果它们为null)? - How can I configure a Jackson ObjectMapper to serialize certain fields of certain beans with a custom value if they are null? Jackson ObjectMapper 在转换为 POJO 时忽略某些键 - Jackson ObjectMapper ignore certain keys when converting to POJO 如何在没有杰克逊注释的情况下忽略某些字段进行反序列化? - How to ignore certain fields for deserialization without jackson annotations? 如何在使用Jackson ObjectMapper时忽略pojo注释? - How to ignore pojo annotations while using Jackson ObjectMapper? Jackson ObjectMapper:如何省略(忽略)null 数组元素 [null] - Jackson ObjectMapper: How to omit (ignore) null array element [null] 如何使用 Jackson ObjectMapper 将 map 相同的 @JsonAlias 转换为不同的 object 字段 - How to map same @JsonAlias to different object fields using Jackson ObjectMapper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM