如何使用Spring HATEOAS消耗_embedded资源

时间:2017-01-13 15:17:36

标签: spring spring-hateoas

我正在尝试使用来自第三方服务的以下REST HAL响应:

{
  "id": 51780,
  "name": "Lambeth",
  "description": "",
  "address_id": 54225,
  "website": "",
  "numeric_widget_id": 3602008,
  "currency_code": "GBP",
  "timezone": "Europe/London",
  "country_code": "gb",
  "live": true,
  "_embedded": {
    "settings": {
      "has_services": true,
      "has_classes": true,
      "payment_tax": 0,
      "currency": "GBP",
      "requires_login": false,
      "has_wallets": false,
      "ask_address": true,
      "_links": {
        "self": {
          "href": "https://myhost.com/api/v1/51780/settings"
        }
      }
    }
  },
  "_links": {
    "self": {
      "href": "https://myhost.com/api/v1/company/51780"
    },
    "settings": {
      "href": "https://myhost.com/api/v1/51780/settings"
    }
  }
}

我希望将其映射到这样的类:

public class Company extends ResourceSupport {

    private String name;
    private CompanySettings settings;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public CompanySettings getSettings() {
        return settings;
    }

    public void setSettings(CompanySettings settings) {
        this.settings = settings;
    }
}

这是一个嵌入式项目的类:

public class CompanySettings extends ResourceSupport {

    private String currency;

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }
}

但是我没有运气让嵌入式项目映射到嵌套设置对象。我的代码如下。

RestTemplate restTemplate = new RestTemplate();

HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

ResponseEntity<Resource<Company>> responseEntity = restTemplate.exchange("https://uk.bookingbug.com/api/v1/company/51780",
        HttpMethod.GET, null, new ParameterizedTypeReference<Resource<Company>>() {
        }, Collections.emptyMap());

if (responseEntity.getStatusCode() == HttpStatus.OK) {
    Resource<Company> userResource = responseEntity.getBody();
    Company company = userResource.getContent();
}

非常感谢任何帮助。

0 个答案:

没有答案