忽略杰克逊反序列化的某些字段,而无需更改模型 [英] Ignore some fields deserialization with jackson without changing model

查看:66
本文介绍了忽略杰克逊反序列化的某些字段,而无需更改模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种配置杰克逊解串器以忽略某些字段的方法.我不想通过注释模型来实现这一点,因为它是由另一个项目给出的.我只想通过构造解串器(ObjectMapper)来做到这一点.
有可能吗?

I'm looking for a way to configure jackson deserializer to ignore some fields. I don't want to achieve this by annotating model since It's out given by another project; I just want to do it by constructing deserializer (ObjectMapper) to do so.
Is it possible?

推荐答案

您可以使用Mix-In批注来实现.

You can achieve that using Mix-In annotation.

class ThirdPartyReadOnlyClass {
   private String ignoredPropertyFromThirdParty;

   public String getIgnoredPropertyFromThirdParty() {
      return ignoredPropertyFromThirdParty;
   }
}

abstract class MixIn {
  @JsonIgnore
  String getIgnoredPropertyFromThirdParty();
}

您可以将JSON注释放在MixIn类上,就像将它们放在原始模型类上一样.

You can put json annotations on MixIn class as if you are putting them on original model class.

配置对象映射器

objectMapper.addMixInAnnotations(ThirdPartyReadOnlyClass.class, MixIn.class);

这篇关于忽略杰克逊反序列化的某些字段,而无需更改模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆