[英]How to produce collections of links within the Links section using Spring HATEOAS
I am attempting to produce a JSON output similar to the example below: 我正在尝试产生类似于以下示例的JSON输出:
{
"links": {
"self": { "href": "http://api.com/items" },
"item": [
{ "href": "http://api.com/items/1" },
{ "href": "http://api.com/items/2" }
]
"data": [
{"itemName":"a"},
{"itemName":"b"}
]
}
Notice that item
is the collection of linked objects. 请注意,
item
是链接对象的集合。 How would I go about doing that with Spring HATEOAS? 我将如何使用Spring HATEOAS做到这一点?
UPDATE: Adding the same rel multiple times to my UserResource
gives me the following: 更新:多次向我的
UserResource
添加相同的rel,可以得到以下信息:
"links": [
{
"rel": "self",
"href": "http://localhost:8080/sophia/users/admin"
},
{
"rel": "item",
"href": "http://localhost:8080/sophia/companies/01393048000170/item/1"
},
{
"rel": "item",
"href": "http://localhost:8080/sophia/companies/01393048000170/item/2"
},
{
"rel": "item",
"href": "http://localhost:8080/sophia/companies/01393048000170/item/3"
}
]
Just add multiple links with the very same rel. 只需添加具有相同rel的多个链接即可。
Resource resource = new Resource(data);
resource.add(new Link("…", "item"));
resource.add(new Link("…", "item"));
you're looking to get resources kinda in a HAL+JSON seriazliation format it seems, although you've said "links" and not "_links". 您似乎希望以HAL + JSON序列化格式获取资源,尽管您说的是“链接”而不是“ _links”。 What you are getting them as is the Spring-HATEOAS default.
Spring-HATEOAS的默认设置就是您得到的它们。 You need to use @EnableHypermedia(HAL).
您需要使用@EnableHypermedia(HAL)。 Also you should have application/hal+json in your Accept header, and possibly your produces param of your @RequestMapping attribute.
另外,您还应该在Accept标头中包含application / hal + json,并且可能在@RequestMapping属性中生成产生参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.