via JPARepsitory
リポジトリでSpring Data RESTを使用すると、1対多の関係を持つ2つのエンティティがあります。アソシエーションリンク経由でアクセスすると、エンティティがコンテンツデータオブジェクトで囲まれているのはなぜですか?
@Entity
class Owner {
// owner attributes...
@OneToMany
Set<Item> items;
}
と
@Entity
class Item {
// item attributes...
@ManyToOne
Owner owner;
}
セットアップは、IDが1の所有者である私は、私はその後
{
// item attributes
"_links": {
"self": {
"href": "http://localhost:8080/api/items/5"
},
"item": {
"href": "http://localhost:8080/api/items/5"
},
"owner": {
"href": "http://localhost:8080/api/items/5/owner"
}
}
}
ような何かを得るhttp://localhost:8080/api/items/5
を呼び出すと、ID 5.
でアイテムを持っています、私が電話するとhttp://localhost:8080/api/items/5/owner
が得られます
{
"content": {
// owner attributes
},
"_links": {
"self": {
"href": "http://localhost:8080/api/owners/1"
},
"owner": {
"href": "http://localhost:8080/api/owners/1"
},
"items": {
"href": "http://localhost:8080/api/owners/1/items"
}
}
}
私は(同じエンティティであるが、代わりに連想HREFのアイデンティティのhrefで)http://localhost:8080/api/owners/1
を呼び出す場合しかし、私は所有者が余分content
に包まれて、ここで
{
// owner attributes
"_links": {
"self": {
"href": "http://localhost:8080/api/owners/1"
},
"owner": {
"href": "http://localhost:8080/api/owners/1"
},
"items": {
"href": "http://localhost:8080/api/owners/1/items"
}
}
}
のような何かを得ます単一の関連付けリソースから呼び出されたときのオブジェクト。私はまったく同じ表現を取り戻すだろうが、この場合には、私にはないself
のhrefをコールした場合、私は期待
を明確にする
を更新。私は標準的なエンティティ表現を取得します。
私の質問は、なぜこの場合ですか?エンティティがアソシエーションリソースを介して返された場合、エンティティが取得されたURIまたはエンティティがアソシエーションリソースを介して返すURIは、エンティティのアイテムリソースのものと同じ表現を持つべきであるself
hrefがあるか?要するに
、私はhttp://localhost:8080/api/items/5/owner
を呼び出すときに私は2つの
{
"content": {
// owner attributes
},
"_links": {
"self": {
"href": "http://localhost:8080/api/items/5/owner"
// ^^^ the URI that was retrieved that will always return this representation
},
"owner": {
"href": "http://localhost:8080/api/owners/1"
// ^^^ the canonical entity URI
},
"items": {
"href": "http://localhost:8080/api/owners/1/items"
}
}
}
OR
{
// owner attributes
"_links": {
"self": {
"href": "http://localhost:8080/api/owners/1"
},
"owner": {
"href": "http://localhost:8080/api/owners/1"
},
"items": {
"href": "http://localhost:8080/api/owners/1/items"
}
}
}
// ^^^ The canonical entity representation (no "content" wrapper)
ではなく、ミックスを得ることを期待します。
オーナーのURIではなく、なぜhttp:// localhost:8080/api/items/5/owner'を呼び出すのですか? – zeroflagL
私はItemから 'merchant'リンクを使用しています。私は質問を更新します。 – Strengthiness