如何告诉 jackson 忽略我无法控制源代码的属性? [英] How can I tell jackson to ignore a property for which I don't have control over the source code?

查看:28
本文介绍了如何告诉 jackson 忽略我无法控制源代码的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长话短说,我的一个实体有一个 GeometryCollection 在您调用getBoundary"时抛出异常(这是另一本书的原因,现在假设它是这样工作的).

Long story short, one of my entities has a GeometryCollection that throws an exception when you call "getBoundary" (the why of this is another book, for now let's say this is the way it works).

有什么方法可以告诉 Jackson 不要包含那个特定的 getter?我知道当我拥有/控制代码时,我可以使用 @JacksonIgnore.但事实并非如此,jackson 通过父对象的连续序列化结束了这一点.我在 jackson 文档中看到了一个过滤选项.这是一个合理的解决方案吗?

Is there a way I can tell Jackson not to include that specific getter? I know I can use @JacksonIgnore when I do own/control the code. But this is not case, jackson ends reaching this point through continuous serialization of the parent objects. I saw a filtering option in jackson documentation. Is that a plausible solution?

谢谢!

推荐答案

您可以使用 Jackson混合.例如:

class YourClass {
  public int ignoreThis() { return 0; }    
}

有了这个 Mixin

abstract class MixIn {
  @JsonIgnore abstract int ignoreThis(); // we don't need it!  
}

有了这个:

objectMapper.getSerializationConfig().addMixInAnnotations(YourClass.class, MixIn.class);

感谢评论,在 Jackson 2.5+ 中,API 发生了变化,应该使用 objectMapper.addMixIn(Class target, Class mixinSource)

Thanks to the comments, with Jackson 2.5+, the API has changed and should be called with objectMapper.addMixIn(Class<?> target, Class<?> mixinSource)

这篇关于如何告诉 jackson 忽略我无法控制源代码的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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