【发布时间】:2019-11-22 03:32:24
【问题描述】:
我得到了 hal 格式的响应:
{
"name": "Publisher A",
"bookPublishers": [
{
"publishedDate": "2019-07-12T08:19:04.583+0000",
"_links": {
"publisher": {
"href": "http://localhost:8080/api/publishers/1"
},
"book": {
"href": "http://localhost:8080/api/books/2"
}
}
},
{
"publishedDate": "2019-07-12T08:19:04.564+0000",
"_links": {
"publisher": {
"href": "http://localhost:8080/api/publishers/1"
},
"book": {
"href": "http://localhost:8080/api/books/1"
}
}
}
],
"_links": {
"self": {
"href": "http://localhost:8080/api/publishers/1"
},
"publisher": {
"href": "http://localhost:8080/api/publishers/1"
},
"friends": {
"href": "http://localhost:8080/api/publishers/1/friends"
},
"createdBy": {
"href": "http://localhost:8080/api/publishers/1/contact"
}
}
}
我在 bookPublishers 和链接中看到 friends 属性。恕我直言,它们都应该是关联链接(请参阅2.4. Creating the Associations),我可以在哪里“放置”其他资源。
我想让 spring 渲染 bookPublishers 和朋友一样。
示例项目在这里:https://github.com/luvarqpp/poc-springHalRelations
你可以这样做:
git clone https://github.com/luvarqpp/poc-springHalRelations.git
cd poc-springHalRelations
mvn clean spring-boot:run
PS:额外的问题,为业务逻辑提供自己的关系最简单的方法是什么,例如关系“renameAuthor”。
【问题讨论】:
-
bookPublishers在响应中内联,因为您没有 BookPublisher 的存储库。如果你想要一个链接,然后创建一个存储库。至于创建附加链接,请参阅:stackoverflow.com/questions/23135756/…
标签: spring-data-rest spring-hateoas