从JSON HAL响应中解析_embedded项目

时间:2016-10-31 10:46:27

标签: java json jackson spring-hateoas

我们正在尝试解析下面的JSON以获取人员列表。

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 ...

基于(非常有限的)客户端文档,这似乎是正确的做事方式。有谁知道我们在这里可能缺少什么?

由于

1 个答案:

答案 0 :(得分:0)

Traverson用于跟踪链接:

  

通过跟踪链接来轻松遍历超媒体API的组件   关系类型。受到同名JavaScript的启发   库。

您的回复中没有与rel 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();