self
href
クライアント側の HAL リソースからを使用して、CRUD 操作の正しいパスを見つけます。単一(トン)のリソースでは、これは正常に機能しています(以下のアドレスリソースを参照してください。_links
含まself
href
れている は埋め込みリソースに含まれています)が、コレクションになると、これは別の話です. _links
コレクションが にある場合、コレクションの はレンダリングされません_embedded
。
最初の子から URL を読み取ることで、この問題を回避しました。しかし、これでは十分ではありません。コレクションが空の場合、そのようなURLを抽出する可能性がない空の配列しかありません。コレクションに新しいアイテムを作成したい場合、 fromPOST
を読み取ることで、クライアントがデータの送信先を知っていることを望みます。を次のようにコレクションに含めることをお勧めします。self
href
_links
_links
{
"_links": {
"self": {
"href": "http://example.com/api/v1/users/1"
}
},
"_embedded": {
"contacts": {
これで、ここで自己 href にアクセスできます。
"_links": {
"self": {
"href": "http://example.com/api/v1/users/1/contacts"
}
},
"_embedded": {
"contacts": [
{
"_links": {
"self": {
"href": "http://example.com/api/v1/users/1/contacts/2"
}
},
"id": "2",
"name": "John Smith"
},
{
"_links": {
"self": {
"href": "http://example.org/api/v1/users/1/contacts/3"
}
},
"id": "3",
"name": "Jane Doe"
}
],
}
},
"address": {
"_links": {
"self": {
"href": "http://example.com/api/v1/addresses/1"
}
},
"street": "Bakerstreet 11",
"postal code": "123456",
"city": "Some city",
"country": "Some country",
}
},
"id": "1",
"name": "John Doe"
}
編集(1年後)
最後に、埋め込みリソースのリンクを常に親リソースに追加することでこれを解決しました。したがって、上記の例では、応答オブジェクトは次のようになります。
{
"_links": {
"self": {
"href": "http://example.com/api/v1/users/1"
},
"contacts": {
"href": "http://example.com/api/v1/users/1/contacts"
},
"address": {
"href": "http://example.com/api/v1/addresses/1"
}
},
"_embedded": {
"contacts": [
{
"_links": {
"self": {
"href": "http://example.com/api/v1/users/1/contacts/2"
}
},
"id": "2",
"name": "John Smith"
},
{
"_links": {
"self": {
"href": "http://example.org/api/v1/users/1/contacts/3"
}
},
"id": "3",
"name": "Jane Doe"
},
],
"address": {
"_links": {
"self": {
"href": "http://example.org/api/v1/addresses/1"
}
},
"street": "Bakerstreet 11",
"postal code": "123456",
"city": "Some city",
"country": "Some country",
}
},
"id": "1",
"name": "John Doe"
}
そのため、リソースを埋め込んでいるかどうかに関係なく、それらがどこにあるかを常に把握しています。連絡先コレクションについては、配列内のコレクション エンドポイントへのリンク_links
と、連絡先自体へのリンクを_embedded
.