3.9.11.4. 获取实体实例列表

假设系统有一个 sales$Order 实体,我们需要得到这个实体实例的列表。还有,我们需要得到的不是所有记录,而只是从第 100 条记录开始的 50 条记录。响应必须不仅包含 sales$Order 实体的简单属性,还包含有关客户的信息(名为 customer 的引用字段)。订单必须按日期排序。

获取 sales$Order 实体的所有实例的基本 URL 如下:

http://localhost:8080/app/rest/v2/entities/sales$Order

要实现上述所有条件,必须指定以下请求参数:

  • view - 一个用于加载实体的视图。在我们的例子中,order-edit-view 包含一个 customer 引用。

  • limit - 要返回的实体数。

  • offset - 第一个被提取记录的位置。

  • sort - 将用于排序的实体属性名称。

必须将 OAuth 令牌放在带有 Bearer 类型的 Authorization 请求头中:

  1. Authorization: Bearer 29bc6b45-83cd-4050-8c7a-2a8a60adf251

这样,我们获得以下 GET 请求 URL:

http://localhost:8080/app/rest/v2/entities/sales$Order?view=order-edit-view&limit=50&offset=100&sort=date

使用 cURL,请求将如下所示:

  1. curl -H "Authorization: Bearer d335902c-9cb4-455e-bf92-24ca1d66d72f" http://localhost:8080/app/rest/v2/entities/sales$Order?view=order-edit&limit=50&offset=100&sort=date

响应将是这样的:

  1. [
  2. {
  3. "_entityName": "sales$Order",
  4. "_instanceName": "00001",
  5. "id": "46322d73-2374-1d65-a5f2-160461da22bf",
  6. "date": "2016-10-31",
  7. "description": "Vacation order",
  8. "number": "00001",
  9. "items": [
  10. {
  11. "_entityName": "sales$OrderItem",
  12. "_instanceName": "Beach umbrella",
  13. "id": "95a04f46-af7a-a307-de4e-f2d73cfc74f7",
  14. "price": 23,
  15. "name": "Beach umbrella"
  16. },
  17. {
  18. "_entityName": "sales$OrderItem",
  19. "_instanceName": "Sun lotion",
  20. "id": "a2129675-d158-9e3a-5496-41bf1a315917",
  21. "price": 9.9,
  22. "name": "Sun lotion"
  23. }
  24. ],
  25. "customer": {
  26. "_entityName": "sales$Customer",
  27. "_instanceName": "Toby Burns",
  28. "id": "4aa9a9d8-01df-c8df-34c8-c385b566ea05",
  29. "firstName": "Toby",
  30. "lastName": "Burns"
  31. }
  32. },
  33. {
  34. "_entityName": "sales$Order",
  35. "_instanceName": "00002",
  36. "id": "b2ad3059-384c-3e03-b62d-b8c76621b4a8",
  37. "date": "2016-12-31",
  38. "description": "New Year party set",
  39. "number": "00002",
  40. "items": [
  41. {
  42. "_entityName": "sales$OrderItem",
  43. "_instanceName": "Jack Daniels",
  44. "id": "0c566c9d-7078-4567-a85b-c67a44f9d5fe",
  45. "price": 50.7,
  46. "name": "Jack Daniels"
  47. },
  48. {
  49. "_entityName": "sales$OrderItem",
  50. "_instanceName": "Hennessy X.O",
  51. "id": "c01be87b-3f91-7a86-50b5-30f2f0a49127",
  52. "price": 79.9,
  53. "name": "Hennessy X.O"
  54. }
  55. ],
  56. "customer": {
  57. "_entityName": "sales$Customer",
  58. "_instanceName": "Morgan Collins",
  59. "id": "5d111245-2ed0-abec-3bee-1a196da92e3e",
  60. "firstName": "Morgan",
  61. "lastName": "Collins"
  62. }
  63. }
  64. ]

请注意,响应中的每个实体都有一个带有实体名称的 _entityName 属性和一个带有实体实例名_instanceName 属性。