没有默认构造函数的 Jackson 3rd Party Class

编程入门 行业动态 更新时间:2024-10-27 22:26:44
本文介绍了没有默认构造函数的 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

本文发布于:2023-11-08 13:19:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Jackson   Class   Party

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!