使用 spring-hateoas 使用基于 HAL 的 REST 服务 [英] Consuming HAL-based REST service with spring-hateoas

查看:63
本文介绍了使用 spring-hateoas 使用基于 HAL 的 REST 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RestTemplate 类使用基于 HAL 的 REST 服务.响应正文如下所示:

I'm trying to consume a HAL-based REST service with the RestTemplate class. The response body looks like this:

{
  "_embedded": {
    "school:teachers": [
      {
        "name": "Adams",
        "state": "CA",
        "_links": {
          "self": {
            "href": "http://localhost:8080/api/teachers/1"
          }
        }
      },
      {
        "name": "Barnes",
        "state": "FL",
        "_links": {
          "self": {
            "href": "http://localhost:8080/api/teachers/2"
          }
        }
      },
      {
        "name": "Armstrong",
        "state": "GA",
        "_links": {
          "self": {
            "href": "http://localhost:8080/api/teachers/3"
          }
        }
      }
    ]
  },
  "_links": {
    "curies": [
      {
        "href": "http://localhost:8080/docs/html5/{rel}.html",
        "name": "school",
        "templated": true
      }
    ]
  }
}

Teacher 类如下所示:

public class Teacher {
    private String name;
    private String state;

    // getters and setters...
}

GET 方法的 REST 服务的返回类型为 ResponseEntity>>.因此,我在客户端代码中的请求如下所示:

The return type of the REST service for GET method is ResponseEntity<Resources<Resource<Component>>>. Therefore my request in the client code looks like this:

...
RestTemplate restTemplate = new RestTemplate();

Map<String, Object> dummy = new HashMap<>();
HttpEntity<String> httpEntity = getHttpEntity(); 

ResponseEntity<Resources<Resource<Teacher>>> response = restTemplate.exchange(url,
                HttpMethod.GET,
                httpEntity,
                new ParameterizedTypeReference<Resources<Resource<Teacher>>>() {});

Resources<Resource<Teacher>> resources = response.getBody();
...

当我使用响应类型 ParameterizedTypeReference 发出请求时,响应的内容为空.当我使用 String 执行此操作时,我会检索内容列表.

When i make the request with the response type ParameterizedTypeReference the content of the response is empty. When i do it with String i retrieve the content list.

我应该如何使用响应类型 ParameterizedTypeReference 来检索它以将其直接映射到 POJO 中?

How should i do the request to retrieve it with the response type ParameterizedTypeReference to map it directly in the POJO?

推荐答案

我尝试了与 Vishnoo Rath 类似的方法.我计划为我的所有资源构建一个通用方法来执行此操作.

I tried a similar approach to what Vishnoo Rath. I am planning to build a common method to do this for all my resources.

ResponseEntity<String> response = 
                restTemplate.exchange("http://localhost:8081/rest/cars", HttpMethod.GET, null, String.class);

        String data = response.getBody();
        //log.info(data);

        ObjectMapper om = new ObjectMapper();
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        JsonNode jsNode = om.readTree(data);
        String test = jsNode.at("/_embedded/cars").toString();
        //log.info(test);

        ArrayList<Car> cars = om.readValue(test, new TypeReference<List<Car>>() {
        });

        for (Car theCar : cars) {
            log.info(">>> " + theCar.getMake() + " " + theCar.getModel() + " " + theCar.getYear());
        }

这篇关于使用 spring-hateoas 使用基于 HAL 的 REST 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆