Jackson 会抛出循环异常吗?

发布于 2025-01-01 12:21:48 字数 1033 浏览 3 评论 0原文

Jackson 是否处理/抛出循环异常,例如当检测到要转换的 java 对象中的循环时,在 json-lib 中抛出 net.sf.json.JSONException: There is a Cycle in the Hierarchy! 异常在 json 中。如果是的话我们该如何处理。

json-lib 中抛出的错误如下

    1169 SEVERE: Servlet.service() for servlet JSONControllerServletGZIP threw exception
    1170 net.sf.json.JSONException: There is a cycle in the hierarchy!
    1171     at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsArray(CycleDetectionStra
    1172     at net.sf.json.JSONArray._fromCollection(JSONArray.java:749)
    1173     at net.sf.json.JSONArray.fromObject(JSONArray.java:165)
    1174     at net.sf.json.JSONObject._processValue(JSONObject.java:2132)
    1175     at net.sf.json.JSONObject._setInternal(JSONObject.java:2177)
    1176     at net.sf.json.JSONObject.setValue(JSONObject.java:1005)
    1177     at net.sf.json.JSONObject._fromMap(JSONObject.java:886)
    1178     at net.sf.json.JSONObject.fromObject(JSONObject.java:248)

提前致谢。

Does jackson Handles/throws the Cyclic exception like net.sf.json.JSONException: There is a cycle in the hierarchy! thrown in json-lib when it detects the cycle in java object that is to be converted in json. If so how can we handle it.

Error thrown in json-lib is as follow

    1169 SEVERE: Servlet.service() for servlet JSONControllerServletGZIP threw exception
    1170 net.sf.json.JSONException: There is a cycle in the hierarchy!
    1171     at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsArray(CycleDetectionStra
    1172     at net.sf.json.JSONArray._fromCollection(JSONArray.java:749)
    1173     at net.sf.json.JSONArray.fromObject(JSONArray.java:165)
    1174     at net.sf.json.JSONObject._processValue(JSONObject.java:2132)
    1175     at net.sf.json.JSONObject._setInternal(JSONObject.java:2177)
    1176     at net.sf.json.JSONObject.setValue(JSONObject.java:1005)
    1177     at net.sf.json.JSONObject._fromMap(JSONObject.java:886)
    1178     at net.sf.json.JSONObject.fromObject(JSONObject.java:248)

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫雨熙 2025-01-08 12:21:48

不,杰克逊不会跟踪循环实例,除了捕获的自引用的特殊情况。正如所建议的,目前处理此问题的最佳方法是使用注释,至少在您有父/子样式引用的情况下。

No, Jackson does not keep track of cyclic instances, except for special case of self-reference which is caught. As suggested, currently best way to deal with this is by use of annotations, at least in cases where you have parent/child style reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文