2016-04-14 154 views
1

予有這種響應格式從後端(URL是http://localhost:8080/App/api/currency):角資源與HATEOAS和應用/ HAL + JSON

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

要使用角查詢它我使用$resource's查詢方法。但是,此響應結構不適用於$resource

  • 查詢需要數組。在這裏,我有一個對象
  • 陣列項目作爲單一資源

我怎樣才能使與$resource即此響應工作指導其採取content作爲構建$resource對象的實際數據處理。還是有任何強大的解決方案呢?

+0

您可以添加一個響應變換器。反應並不是真正的'hal + json'順便說一句。該配置可能缺少一些東西。 – zeroflagL

回答

0

一種方法是使用$promise明確:

return $resource(url).get().$promise.then(function(response){ 
    return response.content; 
}); 

當然,使用$resource當這種方式,有沒有與$http太大的區別,並且使用$resource超過$http,如直接結合的結果的優點範圍,都丟失了。

+0

我想你錯過了我的觀點:我沒有$資源的客戶端代碼手動執行+我不解決「數組項目被視爲單一資源」的問題 – dragonfly