2017-05-10 5 views
3

via 데이터를 통해 JPARepsitory 리포지토리의 REST를 사용하면 일대 다 관계가있는 엔티티가 두 개 있습니다. 내가 http://localhost:8080/api/items/5 전화 나는 그런연관 링크를 통해 액세스 할 때 컨텐츠 객체에 포함 된 엔티티가 스프링 데이터 REST를 갖는 이유는 무엇입니까?

{ 
    // 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" 
    } 
    } 
} 

같은 것을 얻을 때

@Entity 
class Owner { 

    // owner attributes... 

    @OneToMany 
    Set<Item> items; 
} 

@Entity 
class Item { 

    // item attributes... 

    @ManyToOne 
    Owner owner; 
}  

셋업은 ID 1 소유자는 ID 5.

와 아이템을 가지고 전화 할 때 http://localhost:8080/api/items/5/owner 가면

나는 (동일 법인이지만, 대신 협회 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" 
    } 
    } 
} 

그러나, 나는 뭔가 여기

{ 
    // 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" 
    } 
    } 
} 

처럼, 소유자가 추가 content에 싸여 수 단수의 연관 자원에서 호출 될 때 객체. 내가 똑같은 표현을 다시 얻을 것 self HREF를 호출한다면

업데이트

내가 기대 명확하게, 그러나이 경우 나는하지 않습니다. 표준 엔티티 표현을 얻습니다.

제 질문은 이것이 이유가 무엇입니까? 엔티티가 검색된 URI 인 self href를 가지고 있거나 엔티티가 연관 자원을 통해 리턴 한 엔티티는 엔티티의 항목 자원과 동일한 표현을 가져야합니까? 한마디로

, 내가 http://localhost:8080/api/items/5/owner를 호출 할 때 나는 두 가지의

{ 
    "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" 
    } 
    } 
} 

또는

{ 
    // 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