没有默认构造函数的 Jackson 3rd Party Class [英] Jackson 3rd Party Class With No Default Constructor

查看:17
本文介绍了没有默认构造函数的 Jackson 3rd Party Class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Jackson 从 Json 读取/写入我的 POJO.到目前为止,我已经对其进行了配置并为我的课程工作,但第 3 方课程除外.尝试读取 Json 时出现错误:

I'm trying to use Jackson to read/write my POJOs to/from Json. As of right now, I've got it configured and working for my classes, except for a 3rd party class. When trying to read in the Json I get the error:

org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type

经过几次快速的谷歌搜索后,我的班级似乎需要一个 默认构造函数覆盖默认值带注释的构造函数.不幸的是,失败的类来自第 3 方库,并且该类没有默认构造函数,我显然无法覆盖代码.

After a few quick google searches, it appears that my class needs either a default constructor or to override the default constructor with annotations. Unfortunately, the class in which this is failing is from a 3rd party library and that class does not have a default constructor and I obviously cannot over-write the code.

所以我的问题是,我能对此做些什么,还是我运气不好?

So my question is, is there anything I can do about this or am I just out of luck?

谢谢.

推荐答案

你可以使用 Jackson 的 Mix-Ins 功能,加上 Creator 功能.Mix-Ins 功能减轻了对原始第三方代码进行注释的需要,Creator 功能提供了自定义实例创建的机制.

You could make use of Jackson's Mix-Ins feature, coupled with the Creator feature. The Mix-Ins feature alleviates the need to annotate the original third-party code, and the Creator feature provides a mechanism for custom instance creation.

对于更多的自定义,编写自定义解串器并不太复杂.

For yet more customization, it's not too involved to write a custom deserializer.

这篇关于没有默认构造函数的 Jackson 3rd Party Class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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