你好(对不起我的英文)
我正在使用angularjs前端网站,使用SPRING MVC生成json的web服务.spring mvc使用JsonIdentityInfo选项进行seralization,因此每个对象只在json中写入一次,并且每次使用一个引用,例如她有2个"计算机"使用相同的对象"component",所以spring将id设置为第一个组件("@componentID":2)和第二个组件juste id(2):
[
{
"@computerID": 1,
"component": {
"@componentID": 2,
"processor": 2,
"ram": "8g",
"harddrive": "wd"
}
},
{
"@computerID": 3,
"component": 2
}
]
Run Code Online (Sandbox Code Playgroud)
我想要的是 :
[
{
"@computerID": 1,
"owner" : "Mister B",
"component": {
"@componentID": 2,
"processor": 2,
"ram": "8g",
"harddrive": "wd"
}
},
{
"@computerID": 3,
"owner" : "Mister A",
"component": {
"@componentID": 2,
"processor": 2,
"ram": "8g",
"harddrive": "wd"
}
}
]
Run Code Online (Sandbox Code Playgroud)
我做了很多搜索代码谁做了这个,但我没有发现任何想法.
我无法编辑Web服务以删除此行为.我可以使用javascript或jquery(或其他librairie)编辑客户端的json,以用真实引用的对象替换引用吗?(数据实际上更复杂,更深,我在对象中有3级子对象).
非常感谢.
有没有办法用@JsonIdentityInfo影响序列化过程,以便它插入整个对象而不是引用id?
@Entity
@JsonIdentityInfo(
generator = ObjectIdGenerators.IntSequenceGenerator.class,
property = "linkLabel")
public class LinkLabel implements Serializable {
//...
}
Run Code Online (Sandbox Code Playgroud)
因此,杰克逊应该包含整个对象,而不是引用id为1的"otherObj".
{
"objects": [{
"id": 1,
"otherObj": [{
"id": 1,
...
}, {
"id": 3,
...
}]
},
"id": 2,
"otherObj": [1] <-- referencing otherObj with id 1
]
}
Run Code Online (Sandbox Code Playgroud)
像这儿:
{
"objects": [{
"id": 1,
"otherObj": [{
"id": 1,
...
}, {
"id": 3,
...
}]
},
"id": 2,
"otherObj": [{
"id": 1, <-- desired format, whole object
...
}]
]
} …Run Code Online (Sandbox Code Playgroud) 我有2个以下的循环引用类,为了方便,我没有放入getter和setter
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "i")
public class A{
int i;
B b1;
B b2;
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "i")
public class B {
int i;
AI a;
}
Run Code Online (Sandbox Code Playgroud)
如果A.b1和A.b2引用同一个B对象,我得到了序列化的json,如下所示:
{
"i": 1,
"b1": {
"i": 2,
"a": 1
},
"b2": 2
}
Run Code Online (Sandbox Code Playgroud)
但我的预期结果是:
{
"i": 1,
"b1": {
"i": 2,
"a": 1
},
"b2": {
"i": 2,
"a": 1
}
}
Run Code Online (Sandbox Code Playgroud)
我检查了jackson的源代码看起来像以前使用的对象的jackson store id/reference,如果任何其他对象使用相同的引用,那么它将使用id而不是序列化整个对象,如果对象保持在同一个循环链中,那就好了,但是如果他们不会停留在相同的链条中然后就像我的例子所示那样奇怪.
有人可以通过@identityinfo注释帮助我获得预期的结果吗?