Java 如何将@JsonIdentityInfo 与循环引用一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37302816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:10:29  来源:igfitidea点击:

How to use @JsonIdentityInfo with circular references?

javajsonHymansoncircular-referenceHymanson2

提问by Burkhard

I am trying to use the @JsonIdentityInfo from Hymanson 2 as described here.

我试图描述使用@JsonIdentityInfoHyman逊2这里

For testing purposes I created the following two classes:

出于测试目的,我创建了以下两个类:

public class A
{
    private B b;
    // constructor(s) and getter/setter omitted
}
public class B
{
    private A a;
    // see above
}

Of course, the naive approach failes:

当然,天真的方法失败了:

@Test
public void testHymansonJr() throws Exception
{
    A a = new A();
    B b = new B(a);
    a.setB(b);
    String s = JSON.std.asString(a);// throws StackOverflowError
    Assert.assertEquals("{\"@id\":1,\"b\":{\"@id\":2,\"a\":1}}", s);
}

Adding @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")to class A and/or class B does not work either.

添加@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")到 A 类和/或 B 类也不起作用。

I was hoping that I could serialize (and later deserialize) ato something like this: (not too sure about the JSON though)

我希望我可以序列化(然后反序列化)a到这样的东西:(虽然不太确定 JSON)

{
    "b": {
        "@id": 1,
        "a": {
            "@id": 2,
            "b": 1
        }
    }
}

How can I do that?

我怎样才能做到这一点?

采纳答案by Sotirios Delimanolis

It seems Hymanson-jrhas a subset of Hymanson's features. @JsonIdentityInfomust not have made the cut.

似乎Hymanson-jr具有 Hymanson 功能的一个子集。@JsonIdentityInfo一定没有成功。

If you can use the full Hymanson library, just use a standard ObjectMapperwith the @JsonIdentityInfoannotation you suggested in your question and serialize your object. For example

如果您可以使用完整的 Hymanson 库,只需使用您在问题中建议的ObjectMapper带有@JsonIdentityInfo注释的标准并序列化您的对象。例如

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class A {/* all that good stuff */}

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class B {/* all that good stuff */}

and then

进而

A a = new A();
B b = new B(a);
a.setB(b);
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(a));

will generate

会产生

{
    "@id": 1,
    "b": {
        "@id": 2,
        "a": 1
    }
}

where the nested ais referring to the root object by its @id.

其中嵌套a通过其@id.

回答by dalil TALEB

In some cases, it can be necessary to annotate the Id Propertywith @JsonProperty("id")

在某些情况下,可能需要使用@JsonProperty("id")注释Id 属性

For example, in my case, this made my application run correctly.

例如,就我而言,这使我的应用程序正确运行。

回答by Cassio Seffrin

There are several approaches to solve this circular references or infinite recursion issues. This linkexplain in details each one. I have solved my issues including @JsonIdentityInfo annotation above each related entity, although @JsonView is more recent and may it's a better solution depending of your scenery.

有几种方法可以解决这种循环引用或无限递归问题。这个链接详细解释了每一个。我已经解决了我的问题,包括每个相关实体上方的 @JsonIdentityInfo 注释,尽管 @JsonView 是更新的,并且可能是更好的解决方案,具体取决于您的风景。

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")

Or using an IntSequenceGenerator implementation:

或者使用 IntSequenceGenerator 实现:

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class)
@Entity
public class A implements Serializable 
...