使用基于HAL的REST服务与spring-hateoas

时间:2017-08-16 01:55:04

标签: java spring rest spring-hateoas

我正在尝试使用RestTemplate类来使用基于HAL的REST服务。 响应主体看起来像这样:

{
  "_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<Resources<Resource<Component>>>。因此,我在客户端代码中的请求如下所示:

...
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时,我检索内容列表。

如何使用响应类型ParameterizedTypeReference执行检索请求以将其直接映射到POJO?

4 个答案:

答案 0 :(得分:2)

我尝试了类似Vishnoo Rath的方法。我正计划建立一种通用方法来对我所有的资源执行此操作。

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());
        }

答案 1 :(得分:1)

当然,你应该选择Traverson

Traverson client = new Traverson(new URI("http://localhost:8080/api/"), 
         MediaTypes.HAL_JSON);
    Resources<Resource<Teacher>> teachers = client
        .follow("school:teachers")
        .toObject(new ResourcesType<Resource<Teacher>>(){});

https://docs.spring.io/spring-hateoas/docs/current/reference/html/#client.traverson

答案 2 :(得分:0)

我也面临着类似的问题。我选择的解决方法是:

        ResponseEntity<String> response = restTemplate.exchange(
                "http://localhost:8080/payment/search/findByApprovalDate?approvalDate=2017-11-06", HttpMethod.GET,
                null, String.class);

        String data = response.getBody();

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

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

        payments = om.readValue(test, new TypeReference<List<RHPayment>>() {
        });

答案 3 :(得分:0)

我使用Bowman在JAVA中使用JSON + HAL资源。与this article中所示的RestTemplate相比,该库大大简化了资源消耗。