我开发了一个REST API,它可以根据请求返回Object或Single Resource的集合。我还以编程方式在每个对象上添加了“self”链接(Spring Hateos)。
Link self = ControllerLinkBuilder.linkTo(TenantController.class).slash(tenant.getTenantId()).withSelfRel();
tenantResource.add(self);
当返回响应主体时,我在单个资源GET上获得“_links”,其中获取集合的“链接”。为什么会有这种差异?
URL (GET) - http://localhost:8080/tenants
Response (it has "links")
========================
[
{
"tenantId": "56c98ebb42d34bd98763f61a",
"name": "ONGC India",
"abbreviation": "ONGC",
"description": "ONGC India is a Indian Organization",
"links": [
{
"rel": "self",
"href": "http://localhost:8080/tenants/56c98ebb42d34bd98763f61a"
}
]
}
]
==========================================================================
URL (GET) - http://localhost:8080/tenants/56c98ebb42d34bd98763f61a
Response (it has "_links")
==========================
{
"tenantId" : "56c98ebb42d34bd98763f61a",
"name" : "ONGC India",
"abbreviation" : "ONGC",
"description" : "ONGC India is a Indian Organization",
"_links" : {
"self" : {
"href" : "http://localhost:8080/tenants/56c98ebb42d34bd98763f61a"
}
}
}