Jackson JSON 反序列化 - 合成列表 getter
我正在尝试使用 Jackson 反序列化一些最初使用 Jackson 创建的 JSON。该模型有一个合成列表 getter:
public List<Team> getTeams() {
// create the teams list
}
其中列表不是私有成员,而是动态生成的。现在序列化很好,但在反序列化中使用 getTeams,大概是因为 Jackson 看到一个带有可变列表的 getter 并认为它可以将其用作 setter。 getTeams 的内部依赖于 Jackson 尚未填充的其他字段。其结果是 NPE,即我认为顺序是这里的问题之一,但不是我想解决的问题。
因此,我想做的是注释 getTeams,以便它永远不会用作 setter,而是用作 getter。这可能吗?有什么建议吗?
I'm trying to use Jackson to deserialize some JSON originally created using Jackson. The model has a synthetic list getter:
public List<Team> getTeams() {
// create the teams list
}
where the list is not a private member, but generated on the fly. Now this serialises fine, but in deserialization uses getTeams, presumably because Jackson sees a getter with a mutable list and thinks it can use it as a setter. The internals of getTeams rely on other fields which Jackson hasn't yet populated. The result of which is a NPE i.e. I think order is one of the problems here but not one I want to solve.
So, what I'd like to do is annotate getTeams so that it's never used as a setter but is used as a getter. Is this possible? Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
禁用
DeserializationConfig.Feature .USE_GETTERS_AS_SETTERS
。使用静态导入可以使这一行更短。
或者,如果您希望注释仅配置此属性的内容,而不是如上所述指定全局设置,则将某些内容标记为“团队”的设置器。
Disable
DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS
.Use static import to make this line shorter.
Or, if you want an annotation to just configure things for this one property, and not specify the global setting as above, then mark something as the setter for "teams".