没有默认构造函数的杰克逊第三方类 [英] Jackson 3rd Party Class With No Default Constructor

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

问题描述

我正在尝试使用Jackson来向/从Json读取/写入我的POJO。截至目前,除了第三方课程外,我已经为我的课程配置并工作了。当试图读入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

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

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功能,加上创作者功能。 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.

这篇关于没有默认构造函数的杰克逊第三方类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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