Consuming HAL-based REST service with spring-hateoas

人走茶凉 提交于 2019-12-10 11:55:41

问题


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
      }
    ]
  }
}

The Teacher class looks like this:

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

    // getters and setters...
}

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

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.

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


回答1:


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



回答2:


Definitely, you should go with 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




回答3:


I too faced a similar problem. And the way I chose to work around it is :

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



回答4:


I use Bowman to consume JSON+HAL resources in JAVA. This library greatly simplifies the consumption of resources comparing to RestTemplate as shown in this article.



来源:https://stackoverflow.com/questions/45703962/consuming-hal-based-rest-service-with-spring-hateoas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!