Jackson转换为json时无限递归
当尝试将具有以下结构的自定义对象 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 (出于特定原因),并且我有这个导致问题的 getValueArray
, Jackson
在转换为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个工作正常的解决方法,因为问题是 Jackson 使用此函数
getValueArray()
作为序列化中的 getter,因为它的名称get Something
,我使用@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 nameget something
, I used@JsonGetter("valueArray")
lets me force Jackson to use the above function as getter for the non exist propertyvalueArray
which return the Value, now jackson will no longer usesgetValueArray()
function as getter.