Hateoas和application / hal + json

时间:2016-04-14 06:42:48

标签: angularjs hateoas spring-hateoas

我有来自后端的这种响应格式(URL为http://localhost:8080/App/api/currency):

{
  "content": [
    {
      "code": "CHF",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/CHF"
        }
      ]
    },
    {
      "code": "EUR",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/EUR"
        }
      ]
    },
    {
      "code": "USD",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/USD"
        }
      ]
    },
    {
      "code": "AUD",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/AUD"
        }
      ]
    },
    {
      "code": "CAD",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/CAD"
        }
      ]
    },
    {
      "code": "GBP",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/GBP"
        }
      ]
    },
    {
      "code": "NZD",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/NZD"
        }
      ]
    },
    {
      "code": "JPY",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/JPY"
        }
      ]
    },
    {
      "code": "SEK",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/SEK"
        }
      ]
    },
    {
      "code": "NOK",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/NOK"
        }
      ]
    },
    {
      "code": "ISK",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/ISK"
        }
      ]
    },
    {
      "code": "DKK",
      "_links": [
        {
          "rel": "self",
          "href": "http://localhost:8080/App/api/currency/DKK"
        }
      ]
    }
  ],
  "_links": [
    {
      "rel": "self",
      "href": "http://localhost:8080/App/api/currency"
    }
  ]
}

要使用Angular查询它我使用$resource's查询方法。但是这种响应结构不适用于$resource

  • 查询需要数组。这里我有单个对象
  • 数组项被视为单个资源

如何使此回复与$resource一起使用,即指示它将content作为构建$resource对象的实际数据。或者有任何可靠的解决方案吗?

1 个答案:

答案 0 :(得分:0)

一种方法是明确使用<div class="content-section-b"> <div class="row"> <div class="col-lg-12">...

$promise

当然,以这种方式使用return $resource(url).get().$promise.then(function(response){ return response.content; }); 时,$resource$http没有太大区别,使用$resource优于$http的优势,就像直接使用{{1}}一样将结果绑定到范围,将丢失。