2017-05-10 34 views
3

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) 

ではなく、ミックスを得ることを期待します。

+0

オーナーのURIではなく、なぜhttp:// localhost:8080/api/items/5/owner'を呼び出すのですか? – zeroflagL

+0

私はItemから 'merchant'リンクを使用しています。私は質問を更新します。 – Strengthiness

答えて

1

http://localhost:8080/api/items/5/ownerは、いわゆるassociation resourceを指す。それは "本当の"オーナーリソースを指していません。

アソシエーションを処理するのが目的です。例えば。 DELETEでリクエストを送信すると、アイテムの所有者のみが削除されますが、実際の所有者エンティティは削除されません。

設定に応じて、所有者のすべての属性を埋め込むことができます。それはあなたがcontentとして得るものです。

+0

私はそれが協会のリソースだということがわかります。ドキュメンテーションは、関連リソースURLからのエンティティの表現が、そのアイデンティティURLからのエンティティの表現とは異なることに言及していない。また、 '_links'のURLは同じです。アソシエーションリソースレスポンスから返された 'self'リンクを呼び出すことはできず、同じレスポンスを返します。 – Strengthiness

関連する問題