Mik*_*e Q 2 java collections rest children url-design
REST最佳实践的一部分是利用响应中的链接以允许客户端从一个实体导航到另一个实体.
例如,如果我有一个具有子帐户的客户对象类型.如果我要求客户使用,/customers/1那么我可能会提供以下回复
{
"self": "http://localhost:43002/rest/v1/customers/1",
"id": 1,
"name": "Isabella Button",
"number": "000001",
"forename": "Isabella",
"surname": "Button",
"accounts": [
{
"self": "http://localhost:43002/rest/v1/accounts/1",
"id": 1,
"name": "Main Account",
"number": "000001",
"currency": "GBP",
"fromDate": "2013-01-01",
"toDate": "9999-01-01",
"createdDttm": "2013-01-01T00:00:00.000"
}
]
}
Run Code Online (Sandbox Code Playgroud)
请注意,self酒店保留了链接.
但是,假设我不想在客户查询中返回帐户,也许帐户数量可能非常大,所以我不想在默认情况下返回它们.
{
"self": "http://localhost:43002/rest/v1/customers/1",
"id": 1,
"name": "Isabella Button",
"number": "000001",
"forename": "Isabella",
"surname": "Button"
}
Run Code Online (Sandbox Code Playgroud)
客户帐户的资源URL可以是 /customers/1/accounts
但是,客户响应高于客户端将无法发现/customers/1/accounts链接.
是否有最佳做法在响应中提供指向返回资源的"子"集合的超链接?
一种做法是使用这样的links元素:
{
"self": "http://localhost:43002/rest/v1/customers/1",
"id": 1,
"name": "Isabella Button",
"number": "000001",
"forename": "Isabella",
"surname": "Button",
"links" : [
{
"rel" : "http://www.yourapi.com/rels/accounts",
"href" : "http://localhost:43002/rest/v1/customers/1/accounts"
},
{
"rel" : "http://www.yourapi.com/rels/someOtherCollection",
"href" : "http://localhost:43002/rest/v1/customers/1/someOtherCollection",
}
]
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您发现更容易构造/读取响应,则可以使用与链接http标头相同的链接.
| 归档时间: |
|
| 查看次数: |
877 次 |
| 最近记录: |