本文介绍了杰克逊:@JsonIdentityInfo对象而不是id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用@JsonIdentityInfo影响序列化过程,以便它插入整个对象而不是引用id?

Is there a way to influence the serialization process with @JsonIdentityInfo so that it inserts the whole object instead of referencing the id?

@Entity
@JsonIdentityInfo(
        generator = ObjectIdGenerators.IntSequenceGenerator.class,
        property = "linkLabel")
public class LinkLabel implements Serializable {
   //...
}

因此,杰克逊不应该引用ID为1的otherObj整个对象。

So instead of referencing "otherObj" with id 1, Jackson should include the whole object.

{
    "objects": [{
            "id": 1,
            "otherObj": [{
                    "id": 1,
                    ...
                }, {
                    "id": 3,
                    ...
                }]
        },
            "id": 2,
            "otherObj": [1] <-- referencing otherObj with id 1
    ]
}

就像这里:

{
    "objects": [{
            "id": 1,
            "otherObj": [{
                    "id": 1,
                    ...
                }, {
                    "id": 3,
                    ...
                }]
        },
            "id": 2,
            "otherObj": [{
                    "id": 1,  <-- desired format, whole object
                    ...
                }]
    ]
}

我们有双向引用,因此@JsonManagedReference和@JsonBackReference无法正常工作。此处描述了此行为()。

We have bidirectional references, so @JsonManagedReference and @JsonBackReference doesn't work properly. This behavior is described here (http://wiki.fasterxml.com/JacksonFeatureObjectIdentity).

推荐答案

如评论和链接中所述,@ JsonIdentityInfo和Jackson Generators似乎没有选项来启用插入对象ids。

Like said in the comments and from the links, @JsonIdentityInfo and the Jackson Generators doesn't seem to have an option to enable inserting objects instead of ids.

经过更多的研究,我们终于找到了这个:

After more research we finally found this:deserialize Jackson object in JavaScript containing JsonIdentityInfo

这正是我们所拥有的场景和我们现在正在使用JSOG,它针对双向引用进行了优化,它就像服务器和客户端(AngularJS)端的魅力一样。

This was exactly the scenario we had and we are now using JSOG, which is optimized for bidirectional references and it works like a charm on server and client (AngularJS) side.

这篇关于杰克逊:@JsonIdentityInfo对象而不是id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 23:40