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

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

问题描述

长话短说,我的一个实体有一个当你调用getBoundary时抛出异常的GeometryCollection (为什么这是另一本书,现在让我们说这是它的工作方式)。

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).

我有没有办法告诉杰克逊不要包括那个特定的吸气剂?我知道当我拥有/控制代码时我可以使用@JacksonIgnore。但事实并非如此,杰克逊通过连续序列化父对象来达到这一点。我在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 Mixins 。例如:

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天全站免登陆