我正在尝试使用 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
发布评论