2016-10-31 117 views
2

我們試圖解析下面的JSON來獲取人員列表。從JSON解析_embedded項目HAL響應

JSON響應:

{ 
    "_embedded": { 
    "people": [ 
     { 
     "id": 35356, 
     "name": "Jon", 
     "description": "Test", 
     "type": "person", 
     "_links": { 
      "self": { 
      "href": "http://localhost/api/v1/50452/people/35356" 
      }, 
      "items": { 
      "href": "http://localhost/api/v1/50452/items?person_id=35356" 
      }, 
      "enabled_services": [ 
      { 
       "title": "Water Company", 
       "href": "http://localhost/api/v1/50452/services/103890" 
      } 
      ] 
     } 
     }, 
     { 
     "id": 46363, 
     "name": "Kevin", 
     "description": "", 
     "type": "person", 
     "_links": { 
      "self": { 
      "href": "http://localhost/api/v1/50452/people/46363" 
      }, 
      "items": { 
      "href": "http://localhost/api/v1/50452/items?person_id=46363" 
      }, 
      "enabled_services": [ 
      { 
       "title": "Water Company", 
       "href": "http://localhost/api/v1/50452/services/103890" 
      } 
      ] 
     } 
     } 
    ] 
    }, 
    "_links": { 
    "self": { 
     "href": "http://localhost/api/v1/50452/people" 
    } 
    } 
} 

我們的代碼:

ParameterizedTypeReference<Resources<Person>> resource = new ParameterizedTypeReference<Resources<Person>>() {}; 

Traverson traverson = new Traverson(new URI("http://localhost/api/v1/people"), MediaType.APPLICATION_JSON_UTF8); 

// Create our own LinkDiscoverer as our service returns application/json instead of application/json+hal 
List<LinkDiscoverer> linkDiscoverers = new ArrayList<>(); 
linkDiscoverers.add(new JsonPathLinkDiscoverer("$._links..['%s']..href", MediaType.APPLICATION_JSON_UTF8)); 
traverson.setLinkDiscoverers(linkDiscoverers); 

HttpHeaders headers = new HttpHeaders(); 
headers.add("App-Key", Globals.Appkey); 
headers.add("App-Id", Globals.AppId); 

Resources<Person> personResources = traverson.follow("people").withHeaders(headers).toObject(resource); 

但是我們得到了以下錯誤:

java.lang.IllegalStateException: Expected to find link with rel 'people' in response {"_embedded":{"people":[{"id":31350,"name":"Jon Blue","description":"Developer","type":"person","deleted":false,"disabled":false,"company_id":50452,"order":31350,"phone_prefix":"44","_links":{"self":{"href":"http://localhost/api/v1/50452/people/31350"},"items":{"href":"http://localhost/api/v1/50452/items?person_id=31350"},"enabled_services":[{"title":"Water ... 

基礎上(很有限的)客戶端的文檔,這似乎成爲正確的做事方式。有誰知道我們可能會在這裏失蹤?

感謝

回答

0

Traverson是爲了用於後續的聯繫:

Component to ease traversing hypermedia APIs by following links with relation types. Highly inspired by the equally named JavaScript library.

有一個在你的迴應相對people沒有聯繫,所以它不能被遵循。

我能想象你想打電話,有一個鏈接people頂級資源:

Traverson traverson = new Traverson(new URI("http://localhost/api/v1/"), MediaType.APPLICATION_JSON_UTF8); 

否則,我會建議使用RestTemplate獲取資源:

restTemplate.exchange(
       URI.create("http://localhost/api/v1/people"), 
       HttpMethod.GET, 
       new HttpEntity<Void>(headers), 
       resource).getBody();