Jackson转换为json时无限递归

发布于 2025-01-09 14:37:49 字数 1393 浏览 1 评论 0原文

当尝试将具有以下结构的自定义对象 Tag 转换为 json 时,它会给出以下错误: java.lang.IllegalArgumentException: Infinite recursion 因为 getValueArray() 函数

public class Tag {
    private String vr;
    @JsonProperty("Value")
    private JsonNode Value;

    //getters and setters

    public org.primefaces.model.TreeNode getValueArray() throws IOException
    {
        System.out.println("enter check");
        TreeNode root = new DefaultTreeNode();
        constructTree(Value, 1, root);
        return root;
    }
}

我有一个 Value 的 getter,它返回 String 而不是 JsonNode (出于特定原因),并且我有这个导致问题的 getValueArrayJackson 在转换为 json 时调用此函数,而不是使用 getValue (我知道 enter check 会打印到控制台),实际上当我删除时这个函数它可以工作,由于某种原因它调用这个函数并在转换中使用返回的 TreeNode ,我认为问题可能是它与 Jackson treeNode 混淆,所以我回来了org.primefaces.model.TreeNode让它识别出这是primefaces TreeNode而不是Jackson TreeNode,我什至不知道它为什么调用这个功能,以及如何解决这个问题。

错误说

java.lang.IllegalArgumentException: Infinite recursion (StackOverflowError) (through reference chain: org.primefaces.model.DefaultTreeNode["parent"]->org.primefaces.model.DefaultTreeNode["children"]->org.primefaces.model.TreeNodeChildren[0]->org.primefaces.model.DefaultTreeNode["parent"]................. and so on

When trying to convert a custom object Tag which has the below structure to json, it gives me the following error: java.lang.IllegalArgumentException: Infinite recursion because of getValueArray() function

public class Tag {
    private String vr;
    @JsonProperty("Value")
    private JsonNode Value;

    //getters and setters

    public org.primefaces.model.TreeNode getValueArray() throws IOException
    {
        System.out.println("enter check");
        TreeNode root = new DefaultTreeNode();
        constructTree(Value, 1, root);
        return root;
    }
}

I have a getter for Value that returns String instead of JsonNode (for specific reason), and I have this getValueArray that causes the problem, Jackson calls this function when converting to json instead of using getValue (I know that as enter check are printed to console), actually when I remove this function it works, for some reason it calls this function and uses the returned TreeNode in it's conversion, I thought the problem might be that it gets confused with Jackson treeNode, so I returned org.primefaces.model.TreeNode to let it recognizes that this is primefaces TreeNode not Jackson TreeNode, I don't even know why it calls this function, and how to fix this.

the error said

java.lang.IllegalArgumentException: Infinite recursion (StackOverflowError) (through reference chain: org.primefaces.model.DefaultTreeNode["parent"]->org.primefaces.model.DefaultTreeNode["children"]->org.primefaces.model.TreeNodeChildren[0]->org.primefaces.model.DefaultTreeNode["parent"]................. and so on

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

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

发布评论

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

评论(1

兮颜 2025-01-16 14:37:49

我找到了一个工作正常的解决方法,因为问题是 Jackson 使用此函数 getValueArray() 作为序列化中的 getter,因为它的名称 get Something,我使用

@JsonGetter("valueArray")
public JsonNode serializeValue()
{
    return Value;
}

@JsonGetter("valueArray") 让我强制 Jackson 使用上述函数作为不存在的属性 valueArray 的 getter,它返回值,现在 Jackson 将不再使用getValueArray() 函数用作 getter。

I found a work around that works fine, as the problem is that Jackson uses this function getValueArray() as a getter in the serialization, because of it's name get something, I used

@JsonGetter("valueArray")
public JsonNode serializeValue()
{
    return Value;
}

@JsonGetter("valueArray") lets me force Jackson to use the above function as getter for the non exist property valueArray which return the Value, now jackson will no longer uses getValueArray() function as getter.

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