我有一个简单的多对多关系:Owner <-> Book <-> Publisher
我注意到一个奇怪的行为:当我得到特定的Book
时,Publisher
和Owner
(子实体)信息与所有属性一起被提取。然而,在Get
所有Book
s中,孩子的属性缺失。
当我对特定的GET
做Book
时,我得到了Book
及其孩子的所有属性:
cURL -XGET http://localhost:8080/books/isbn/978-0743246264
{
"id":4,
"name":"Book 4",
"isbn":"978-0743246264",
"publishers":[
{
"id":1,
"name":"Publisher 1",
"description":"Description - 1"
}
],
"owners":[
{
"id":3,
"name":"Owner 3"
}
]
}
但是,当我为所有书籍运行GET
时,某些元素缺少子属性:
cURL -XGET http://localhost:8080/books
[
{
"id":1,
"name":"Book 1",
"isbn":"978-0743246261",
"publishers":[
{
"id":1,
"name":"Publisher 1",
"description":"Description - 1"
},
{
"id":2,
"name":"Publisher 2",
"description":"Description - 2"
}
],
"owners":[
{
"id":1,
"name":"Owner 1"
}
]
},
{
"id":2,
"name":"Book 2",
"isbn":"978-0743246262",
"publishers":[
{
"id":4,
"name":"Publisher 4",
"description":"Description - 4"
},
1,
{
"id":3,
"name":"Publisher 3",
"description":"Description - 3"
}
],
"owners":[
{
"id":2,
"name":"Owner 2"
},
{
"id":3,
"name":"Owner 3"
},
1
]
},
{
"id":3,
"name":"Book 3",
"isbn":"978-0743246263",
"publishers":[
4,
2
],
"owners":[
2
]
},
{
"id":4,
"name":"Book 4",
"isbn":"978-0743246264",
"publishers":[
1
],
"owners":[
3
]
}
]
我已经在GitHub上设置了项目,它已经准备好立即运行和测试:https://github.com/tekpartner/learn-spring-boot-many-2-many
从@JsonIdentityInfo和Publisher
类中删除Owner
注释,因此Jackson将序列化该类的完整版本,而不是使用他们的id
作为参考。