2016-10-31 68 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();