【发布时间】:2018-06-11 16:17:46
【问题描述】:
我有一个简单的多对多关系:Owner <-> Book <-> Publisher
我注意到一个奇怪的行为:当我获得特定的 Book 时,Publisher 和 Owner(子实体)信息与所有属性一起被获取。但是,在GetAllBooks 中,缺少孩子的属性。
当我对特定的Book 执行GET 时,我会得到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
【问题讨论】:
-
这显然与您的代码有关。特别是您要返回的课程上的杰克逊注释。但是您没有发布代码,所以...检查您选择放在属性上的注释,并阅读他们的文档。
-
谢谢@JBNizet,你是目标。我修复了它并将代码签入到 GitHub 存储库。
标签: hibernate jpa spring-boot jackson many-to-many