Jackson第三方类,没有默认构造函数

时间:2021-03-20 18:01:49

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:

我正试着用杰克逊来读/写我的pojo /从Json。到目前为止,我已经为我的类配置好了它并开始工作,除了一个第三方类。当尝试读取Json时,我得到了错误:

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?

所以我的问题是,我能做些什么,或者我只是运气不好?

Thanks.

谢谢。

1 个解决方案

#1


23  

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.

您可以使用Jackson的混搭特性,以及Creator特性。mix - in特性减少了注释原始第三方代码的需要,而Creator特性提供了自定义实例创建机制。

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

对于更多的自定义,它不需要编写自定义的反序列化器。

#1


23  

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.

您可以使用Jackson的混搭特性,以及Creator特性。mix - in特性减少了注释原始第三方代码的需要,而Creator特性提供了自定义实例创建机制。

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

对于更多的自定义,它不需要编写自定义的反序列化器。