プロパティの制限

大きなエンティティやコレクションを取得すると、応答のサイズが大きくなり、より多くの帯域幅を使用しますが、応答に含めるフィールドをリクエストでサーバーに伝えることで軽減することができます。これは、スパースフィールドセットとして知られています。スパースフィールドセットを使用してリクエストを行うには、各フィールドの属性名と共に、URLにfieldsパラメータを含めます。

たとえば、以下のリクエストはスパースフィールドセットを使用していないため、ブログ投稿のすべてのフィールドを返します。

curl "http://localhost:8080/o/headless-delivery/v1.0/blog-postings/59301"  -u 'test@liferay.com:test'
{
  "alternativeHeadline": "The power of OpenAPI & Liferay",
  "articleBody": "<p>We are happy to announce...</p>",
  "creator": {
    "familyName": "Test",
    "givenName": "Test",
    "id": 20130,
    "name": "Test Test",
    "profileURL": "/web/test"
  },
  "dateCreated": "2019-04-22T07:04:47Z",
  "dateModified": "2019-04-22T07:04:51Z",
  "datePublished": "2019-04-22T07:02:00Z",
  "encodingFormat": "text/html",
  "friendlyUrlPath": "new-headless-apis",
  "headline": "New Headless APIs",
  "id": 59301,
  "numberOfComments": 0,
  "siteId": 20124
}

見出し、作成日、および作成者の情報のみを取得するには、headlinedateCreated、およびcreatorを含めたfieldsパラメータをURLに追加します。

curl "http://localhost:8080/o/headless-delivery/v1.0/blog-postings/59301?fields=headline,dateCreated,creator"  -u 'test@liferay.com:test'
{
  "creator": {
    "familyName": "Test",
    "givenName": "Test",
    "id": 20130,
    "name": "Test Test",
    "profileURL": "/web/test"
  },
  "dateCreated": "2019-04-22T07:04:47Z",
  "headline": "New Headless APIs"
}

応答では、creator属性はネストされたJSONオブジェクトです。作成者の名前のみを返すには、ネストされたフィールドをドット表記(creator.name)で指定します。

curl "http://localhost:8080/o/headless-delivery/v1.0/blog-postings/59301?fields=headline,dateCreated,creator.name"  -u 'test@liferay.com:test'
{
  "creator": {
    "name": "Test Test"
  },
  "dateCreated": "2019-04-22T07:04:47Z",
  "headline": "New Headless APIs"
}

このfieldsパラメータは、コレクションリソースと連携して、すべてのコレクションアイテムの指定された属性を返します。たとえば、以下のリクエストは20124というIDを持つサイト内のすべてのブログ投稿の見出しを取得します。

curl "http://localhost:8080/o/headless-delivery/v1.0/sites/20124/blog-postings/?fields=headline"  -u 'test@liferay.com:test'
{
  "items": [
    {
      "headline": "New Headless APIs"
    },
    {
      "headline": "Authenticated requests"
    }
  ],
  "lastPage": 1,
  "page": 1,
  "pageSize": 20,
  "totalCount": 2
}

関連トピック

Making Authenticated Requests

API Formats and Content Negotiation

Working with Collections of Data

« フィルタ、ソート、検索マルチパートリクエスト »
この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています