Web Content API
ご覧のページは、お客様の利便性のために一部機械翻訳されています。また、ドキュメントは頻繁に更新が加えられており、翻訳は未完成の部分が含まれることをご了承ください。最新情報は都度公開されておりますため、必ず英語版をご参照ください。翻訳に問題がある場合は、 こちら までご連絡ください。

高度なウェブコンテンツAPI

Liferay DXP RESTサービスを使用すると、サイトの構造化コンテンツを作成および管理できます。 構造化コンテンツは、Web コンテンツ構造 (または構造) を使用する Web コンテンツ です。 構造とは、著者、要約、記事に含まれる内容など、情報を定義するものです。 構造によって、記事に必要な情報がすべて含まれることが保証されます。 構造の詳細については、 Web コンテンツ構造 を参照してください。

構造と構造化コンテンツについて詳しく学ぶには、いくつかの cURL および Java コード サンプルを含む、事前に構築された Liferay DXP Docker イメージを使用してください。

注記

Liferay DXP での REST API の使用方法の概要については、 REST サービスの利用 を、Web コンテンツ API の概要については、 Web コンテンツ API の基本 を参照してください。

環境設定

Liferay DXP の新しいインスタンスを起動するには、以下を実行します。

docker run -it -m 8g -p 8080:8080 liferay/dxp:2026.q1.9-lts

http://localhost:8080 に、メールアドレス test@liferay.com とパスワード test を使用して Liferay にサインインします。 プロンプトが表示されたら、パスワードを learn に変更してください。

次に、 サンプルプロジェクトをダウンロードして解凍します。

curl https://resources.learn.liferay.com/examples/liferay-m7b2.zip -O
unzip liferay-m7b2.zip
注記

これらのスクリプトは基本認証を使用しており、テスト用に設計されています。 Liferay DXPの本番環境では、基本認証を使用しないでください。

サイトIDを特定します

  1. サイト メニュー (Site menuを開き、 設定を展開し、 サイト設定 に移動します。

  2. プラットフォーム セクションで、 サイト構成 をクリックします。

    Liferay DXP バージョン 7.3 以前の場合は、「一般」タブをクリックしてください。

  3. サイト識別子は「サイトID」の項目で確認できます。

    サイト設定とサイト構成オプションでサイトIDを確認してください。

Liferay DXPに画像を追加する

画像を含む様々なコンテンツフィールドを持つ記事から始めましょう。 これらの画像を記事に追加するには、

  1. サイト メニュー (Site menuを開き、 コンテンツ & データを展開し、 ドキュメントとメディア に移動します。

  2. 新規 をクリックし、 複数ファイルアップロード を選択します。

  3. foo.png、 bar.png、および goo.png の画像を サンプル プロジェクト フォルダー からドロップ エリアにドラッグ アンド ドロップします。

  4. をクリックして公開 します。

または、 Documents_POST_ToSites.sh スクリプトを使用して、REST API の Document サービスを使用して各画像を個別にアップロードします。

curl \
   "http://localhost:8080/o/headless-delivery/v1.0/sites/${1}/documents" \
   --form "file=@${2}" \
   --header "Content-Type: multipart/form-data" \
   --request "POST" \
   --user "test@liferay.com:learn"

この例では、サイト ID ({1}) と相対画像ファイルパス ({2}) をパラメータとして使用して、 foo.png 画像を投稿します。

メソッドサービスエンドポイント
GET書類/v1.0/sites/${1}/documents
./Documents_POST_ToSites.sh "../images/foo.png" 20125
パラメーター #説明
$1サイトID
$2ファイルへの相対パス

JSON出力には、 id フィールドに画像識別子が含まれます。

{
...
   "encodingFormat" : "image/png"、
   "externalReferenceCode" : "44339"、
   "fileExtension" : "png"、
   "id" : 44339、
...
   "タイトル" : "foo.png"
}

REST API を使用してドキュメントを操作する方法の詳細については、 ドキュメント API の基本 を参照してください。

または、 Documents_POST_ToSites クラスを使用することもできます。 curlフォルダからjavaフォルダに移動してください。 ソースファイルをコンパイルします。

javac -classpath .:* *.java

Documents_POST_ToSites クラスを実行します。 siteIdの値を自分のサイト ID に、filePathの値をfoo.png画像へのパスに置き換えてください:

java -classpath .:* -DfilePath=1234 -DsiteId=5678 Documents_POST_ToSites

サンプル構造を作成する

注記

REST API を使用してプログラムで構造を作成することはできません。

  1. サイト メニュー (Site menu) を開き、 コンテンツ & データを展開し、 Web コンテンツ に移動します。

  2. 構造 タブを選択し、 新規をクリックします。

  3. 以下の情報を使用して新しい構造を作成してください。

    • 名前: Foo
    • フィールド(この順序): テキスト、 画像、 日付、 単一選択
  4. 単一選択フィールドには、以下の値を使用してください。

    • 最初のオプション値: Foo
    • 2番目のオプション値: Goo

    ユーザーインターフェースに新しい構造を作成します。

  5. 各構造フィールドをクリックし、そのフィールド参照値を更新してください(下表を参照)。 フィールド参照値は、サイドバーの「詳細設定」セクションに記載されています。

  6. をクリックして保存します。

項目新規フィールド参照値
テキストボックステキスト参照
画像画像参照
日付日付参照
選択単一選択参照

フィールド参照値を更新します。

詳細については、 Web コンテンツ構造の作成 を参照してください。

サンプルテンプレートを作成する

注記

REST APIを使用してプログラムでWebコンテンツテンプレートを作成することはできません。

構造体の イメージ フィールドを含むテンプレートを作成します。

  1. サイト メニュー (Site menu) を開き、 コンテンツ & データを展開し、 Web コンテンツ に移動します。

  2. テンプレート タブを選択し、 新規 をクリックします。

  3. テンプレート名として Foo と入力してください。

  4. プロパティサイドパネルから、 をクリックし、構造フィールドの横に (Add)を追加します。

    テンプレートを構造にリンクするには、構造フィールドの横にある「追加」をクリックします。

  5. 前のセクションで説明したFoo構造体を選択してください。

  6. FreeMarker エディタに含まれるデフォルトの <#-- --> ブロック コメントを削除します。

  7. サイドバーで、 要素 (Elements をクリックします。

  8. [フィールド] グループで、 テキスト、 画像、 日付、および 単一選択 フィールドをクリックして、テンプレートに追加します。 FreeMarkerエディタでは、各フィールドが必ず改行で始まるようにしてください。

    FreeMarkerエディタでテンプレートのフィールドを編集します。

  9. をクリックして保存します。

構造体の イメージ フィールドを含まない2つ目のテンプレートを作成します。

  1. テンプレート タブで、 アクション (Actions) をクリックし、 コピー を選択します。

    「アクション」メニューを使用して、最初のテンプレートをコピーします。

  2. テンプレートの名前を Goo に更新し、 Copy をクリックします。

  3. テンプレート タブで、新しい Goo テンプレートをクリックします。

  4. テンプレート エディターで、FreeMarker スクリプトから画像情報 <#if (ImageReference.getData())></#if> を削除します。

  5. をクリックして保存します。

詳細については、 Web コンテンツ テンプレートの作成 を参照してください。

サイトからウェブコンテンツ構造を取得する

指定されたサイトに存在するすべての構造を取得するには、 ContentStructures_GET_FromSites.sh スクリプトを使用します。 このスクリプトは、 ContentStructure サービスと GET HTTP メソッド、および サイト ID パラメータを使用します。

メソッドサービスエンドポイント
GETコンテンツ構造/v1.0/sites/${1}/content-structures
./ContentStructures_GET_FromSites.sh 20125
パラメーター #説明
$1サイトID

JSONレスポンスで、構造 id ("id" : 43563) と name ("name" : "Foo を特定します。") contentStructureFields セクションでは、構造フィールドについて説明します。 この構造には、 TextReference、 ImageReference、 DateReference、および SingleSelectionReference という名前の 4 つの異なるフィールドが含まれています。

{
   "actions" : { },
   "facets" : [ ],
   "items" : [ {
      "availableLanguages" : [ "en-US" ],
      "contentStructureFields" : [ {
         "dataType" : "string",
         "inputControl" : "text",
         "label" : "text",
         "localizable" : true,
         "multiple" : false,
         "name" : "TextReference",
         "nestedContentStructureFields" : [ ],
         "options" : [ ],
         "predefinedValue" : "",
         "repeatable" : false,
         "required" : false,
         "showLabel" : true
      }, {
         "dataType" : "image",
         "label" : "Image",
         "localizable" : true,
         "multiple" : false,
         "name" : "ImageReference",
         "nestedContentStructureFields" : [ ],
         "options" : [ ],
         "predefinedValue" : "{}",
         "repeatable" : false,
         "required" : false,
         "showLabel" : true
      }, {
         "dataType" : "date",
         "label" : "Date",
         "localizable" : true,
         "multiple" : false,
         "name" : "DateReference",
         "nestedContentStructureFields" : [ ],
         "options" : [ ],
         "predefinedValue" : "",
         "repeatable" : false,
         "required" : false,
         "showLabel" : true
      }, {
         "dataType" : "string",
         "inputControl" : "radio",
         "label" : "単一選択",
         "localizable" : true,
         "multiple" : false,
         "name" : "SingleSelectionReference",
         "nestedContentStructureFields" : [ ],
         "options" : [ {
         "label" : "Foo",
         "value" : "Option13142925"
         }, {
         "label" : "Goo",
         "value" : "Option50805674"
         } ],
         "predefinedValue" : "[]",
         "repeatable" : false,
         "required" : false,
         "showLabel" : true
      } ],
      "creator" : {
         "additionalName" : "",
         "contentType" : "UserAccount",
         "familyName" : "Bowman",
         "givenName" : "David",
         "id" : 20129,
         "name" : "David Bowman"
      },
      "dateCreated" : "2021-08-25T07:52:46Z",
      "dateModified" : "2021-08-25T08:01:58Z",
      "description" : "",
      "id" : 43563,
      "name" : "Foo",
      "siteId" : 20125
   } ],
   "lastPage" : 1,
   "page" : 1,
   "pageSize" : 20,
   "totalCount" : 1
}

または、 ContentStructures_GET_FromSites Java クラスを実行します。

java -classpath .:* -DsiteId=1234 ContentStructures_GET_FromSites

画像IDを取得する

以前に投稿された画像の ID を取得するには、 Documents_GET_FromSites.sh スクリプトを使用します。 このスクリプトは、 ドキュメント サービスと GET HTTP メソッド、および サイト ID をパラメータとして使用します。

メソッドサービスエンドポイント
GETドキュメント/v1.0/sites/${1}/documents
./Documents_GET_FromSites.sh 20125
パラメーター #説明
$1サイトID

または、 Documents_GET_FromSites Java クラスを実行します。

java -classpath .:* -DsiteId=1234 Documents_GET_FromSites

サイトからウェブコンテンツテンプレートを取得する

サイトのすべてのテンプレートを取得するには、 ContentTemplates_GET_FromSites スクリプトを使用します。 このスクリプトは、 ContentTemplate サービスと GET HTTP メソッド、および サイト ID をパラメータとして使用します。

メソッドサービスエンドポイント
GETコンテンツテンプレート/v1.0/sites/${1}/content-templates
./ContentTemplates_GET_FromSites.sh 20125
パラメーター #説明
$1サイトID

JSONレスポンスには以下が含まれます

  • 2 つのテンプレート: "name": "Foo" および "name": "Goo"。

  • 構造をテンプレートにリンクする contentStructureId。

  • テンプレートスクリプト には、指定された プログラミング言語 で書かれたスクリプトが含まれます。 この例では、FreeMarkerを使用しています。

  • FooテンプレートのtemplateScriptフィールドでは、<#if (ImageReference.getData())></#if> を使用して、imageフィールドを参照しています。 Goo テンプレートにはこの参照が含まれていません。

{
   "アクション" : {
      "取得" : {
         "メソッド" : "GET"、
         "href" : "http://localhost:8080/o/headless-delivery/v1.0/sites/20125/content-templates"
      }
   },
   "ファセット" : [ ],
   "アイテム" : [ {
      "アクション" : {
         "取得" : {
         "メソッド" : "GET"、
         "href" : "http://localhost:8080/o/headless-delivery/v1.0/sites/20125/content-templates/{contentTemplateId}"
         }
      },
      "availableLanguages" : [ "en-US" ],
      "contentStructureId" : 43563,
      "creator" : {
         "additionalName" : ""、
         "contentType" : "UserAccount"、
         "familyName" : "Bowman"、
         "givenName" : "David",
         "id" : 20129,
         "name" : "David Bowman"
      },
      "dateCreated" : "2021-08-25T13:39:20Z",
      "dateModified" : "2021-08-25T13:39:20Z"、
      "説明" : ""、
      "ID" : "43823"、
      "名前" : "Foo"、
      "プログラミング言語" : "ftl"、
      "siteId" : 20125,
      "templateScript" : "<#if (TextReference.getData())??>\n\t${TextReference.getData()}\n</#if>\n<#if (ImageReference.getData())?? && ImageReference.getData() != \"\">\n\t<img alt=\"${ImageReference.getAttribute(\"alt\")}\" data-fileentryid=\"${ImageReference.getAttribute(\"fileEntryId\")}\" src=\"${ImageReference.getData()}\" />\n</#if>\n<#assign DateReference_Data = getterUtil.getString(DateReference.getData())>\n\n<#if validator.isNotNull(DateReference_Data)>\n\t<#assign DateReference_DateObj = dateUtil.parseDate(\"yyyy-MM-dd\", DateReference_Data, locale)>\n\n\t${dateUtil.getDate(DateReference_DateObj, \"dd MMM yyyy - HH:mm:ss\", locale)}\n</#if>\n<#if (SingleSelectionReference.getData())??>\n\t${SingleSelectionReference.getData()}\n</#if>"
   }, {
      "actions" : {
         "get" : {
         "method" : "GET",
         "href" : "http://localhost:8080/o/headless-delivery/v1.0/sites/20125/content-templates/{contentTemplateId}"
         }
      },
      "availableLanguages" : [ "en-US" ],
      "contentStructureId" : 43563,
      "creator" : {
         "additionalName" : "",
         "contentType" : "UserAccount",
         "familyName" : "Bowman",
         "givenName" : "David",
         "id" : 20129,
         "name" : "David Bowman"
      },
      "dateCreated" : "2021-08-26T10:10:22Z",
      "dateModified" : "2021-08-26T14:08:53Z",
      "description" : "",
      "id" : "44177",
      "name" : "Goo",
      "programmingLanguage" : "ftl",
      "siteId" : 20125,
      "templateScript" : "<#if (TextReference.getData())??>\n\t${TextReference.getData()}\n</#if>\n<#assign DateReference_Data = getterUtil.getString(DateReference.getData())>\n\n<#if validator.isNotNull(DateReference_Data)>\n\t<#assign DateReference_DateObj = dateUtil.parseDate(\"yyyy-MM-dd\", DateReference_Data, locale)>\n\n\t${dateUtil.getDate(DateReference_DateObj, \"dd MMM yyyy - HH:mm:ss\", locale)}\n</#if>\n<#if (SingleSelectionReference.getData())??>\n\t${SingleSelectionReference.getData()}\n</#if>"
   } ],
   "lastPage" : 1,
   "page" : 1,
   "pageSize" : 20,
   "totalCount" : 2
}

または、 ContentTemplates_GET_FromSites Java クラスを実行します。

java -classpath .:* -DsiteId=1234 ContentTemplates_GET_FromSites

ウェブコンテンツテンプレートの入手

特定のテンプレートに関する情報を取得するには、サイトIDとテンプレートIDを必要とする ContentTemplates_GET_ById.sh cURLスクリプトを使用します。

メソッドサービスエンドポイント
GETコンテンツテンプレート/v1.0/sites/${1}/content-templates/${2}
./ContentTemplates_GET_ById.sh 20125 43823
パラメーター #説明
$1サイトID
$2コンテンツテンプレートID

または、 ContentTemplates_GET_ById Java クラスを実行します。 以下の値を、ご自身のサイトIDとテンプレートIDに置き換えてください。

java -classpath .:* -DcontentTemplateId=1234 -DsiteId=5678 ContentTemplates_GET_ById

ウェブコンテンツ記事の投稿

StructuredContents_POST_ToSites.sh cURL スクリプトを使用して、 POST HTTP メソッドを使用して、作成した サンプル構造 を使用して新しい記事を作成します。 このスクリプトは、サイトID、構造体ID、foo.pngの 画像IDをパラメータとして必要とする。

メソッドサービスエンドポイント
PUT構造化コンテンツ/v1.0/sites/{siteId}/structured-contents
./StructuredContents_POST_ToSites.sh 20125 43563 43795

cURLスクリプトのパラメータ:

パラメーター #説明
$1サイトID
$2画像ID
$3contentStructured

Liferay DXP で新しい記事を見つけるには、 サイト メニュー (Site menu) → コンテンツ & データ → Web コンテンツ を開きます。

POST HTTPメソッドで送信された記事がUIに表示されます。

このスクリプトは、以下の属性を持つ新しい記事を投稿します。

  • "タイトル" : "可能"
  • 記事の本文を定義する 4 つの コンテンツフィールド 値:
    • 内のテキスト文字列、テキスト参照。
    • 内の画像、画像参照。
    • 日付情報は DateReference にあります。
    • SingleSelectionReference 内のラジオボタンコントロール。
{
   "アクション" : {
...
   },
   "availableLanguages" : [ "en-US" ],
   "contentFields" : [ {
      "contentFieldValue" : {
         "data" : "このテキストはFooについて説明しています。"
      },
      "dataType" : "string",
      "inputControl" : "text",
      "label" : "Text",
      "name" : "TextReference",
      "nestedContentFields" : [ ],
      "repeatable" : false
   }, {
      "contentFieldValue" : {
         "image" : {
         "contentType" : "Document",
         "contentUrl" : "/documents/20125/0/foo.png/50956e56-9571-8f73-ae6e-9fca20fe0e3a?t=1629897455431",
         "description" : "このテキストはFooの画像について説明しています。",
         "encodingFormat" : "image/png",
         "fileExtension" : "png",
         "id" : 43795,
         "sizeInBytes" : 6232,
         "title" : "Foo"
         }
      },
      "dataType" : "image",
      "label" : "Image",
      "name" : "ImageReference",
      "nestedContentFields" : [ ],
      "repeatable" : false
   }, {
      "contentFieldValue" : {
         "data" : "2021-08-30T00:00:00Z"
      },
      "dataType" : "date",
      "label" : "Date",
      "name" : "DateReference",
      "nestedContentFields" : [ ],
      "repeatable" : false
   }, {
      "contentFieldValue" : {
         "data" : "Foo"
      },
      "dataType" : "string",
      "inputControl" : "radio",
      "label" : "Single Selection",
      "name" : "SingleSelectionReference",
      "nestedContentFields" : [ ],
      "repeatable" : false
   } ],
   "contentStructureId" : 43563,
   "creator" : {
      "additionalName" : "",
      "contentType" : "UserAccount",
      "familyName" : "Bowman",
      "givenName" : "David",
      "id" : 20129,
      "name" : "David Bowman"
   },
   "customFields" : [ ],
   "dateCreated" : "2021-08-26T06:14:06Z",
   "dateModified" : "2021-08-26T06:14:06Z",
   "datePublished" : "2021-08-26T06:14:00Z",
   "description" : "",
   "externalReferenceCode" : "43847",
   "friendlyUrlPath" : "able",
   "id" : 43849,
   "key" : "43847",
   "keywords" : [ ],
   "numberOfComments" : 0,
   "relatedContents" : [ ],
   "renderedContents" : [ {
      "contentTemplateId" : "43823",
      "contentTemplateName" : "Foo",
      "markedAsDefault" : true,
      "renderedContentURL" : "http://localhost:8080/o/headless-delivery/v1.0/structured-contents/43849/rendered-content/43823"
   }, {
      "contentTemplateId" : "43868",
      "contentTemplateName" : "Goo",
      "markedAsDefault" : false,
      "renderedContentURL" : "http://localhost:8080/o/headless-delivery/v1.0/structured-contents/44060/rendered-content/43868"
   } ],
   "siteId" : 20125,
   "subscribed" : false,
   "taxonomyCategoryBriefs" : [ ],
   "title" : "Able",
   "uuid" : "01ace059-814a-132d-d46d-21737ef2ec53"
}

または、 StructuredContents_POST_ToSites Java クラスを実行します。 以下の値を、ご自身の構造、サイト、およびイメージのIDに置き換えてください。

java -classpath .:* -DcontentStructureId=1234 -DimageId=5678 -DsiteId=91011 StructuredContents_POST_ToSites

特定のテンプレートを使用してWebコンテンツ記事をレンダリングする

記事は特定のテンプレートにリンクされていません。 テンプレートは、記事の表示方法を定義します。 同じ記事でも、異なるテンプレートを使用できます。 詳細については、 Webコンテンツ構造 を参照してください。

ヒント

記事は特定のテンプレートにリンクされていないため、新しい記事を POST 送信する際にテンプレートを指定することはできません (HTTP POST メソッドは、テンプレートを記述する renderedContents セクションを無視します)。

選択した記事とテンプレートを使用して記事をレンダリングするには、 ./StructureContents_GET_RenderedContent_ById.sh スクリプトを使用します。 このスクリプトは、 StructuredContent サービスで GET HTTP メソッドを使用します。

メソッドサービスエンドポイント
PUT構造化コンテンツ/v1.0/structured-contents/${1}/rendered-content/${2}
./StructureContents_GET_RenderedContent_ById.sh 43849 43868

以下は、 画像 フィールドを含むテンプレートを使用したスクリプトの出力です。

Foo<picture data-fileentryid="43795"><source media="(max-width:300px)" srcset="http://localhost:8080/o/adaptive-media/image/43795/Thumbnail-300x300/foo.png?t=1629897455431, /o/adaptive-media/image/43795/Preview-1000x0/foo.png?t=1629897455431 2x" /><source media="(max-width:600px) and (min-width:300px)" srcset="http://localhost:8080/o/adaptive-media/image/43795/Preview-1000x0/foo.png?t=1629897455431" /><img alt="Foo alt-image description" data-fileentryid="43795" src="http://localhost:8080/documents/20125/0/foo.png/50956e56-9571-8f73-ae6e-9fca20fe0e3a?t=1629897455431" /></picture>2021年8月30日 - 00:00:00Option1314292

テンプレートを 画像 フィールドなしで指定すると、 <picture></picture> の情報は出力に表示されません。 画像 フィールドなしでテンプレートを使用した場合のスクリプト出力は次のとおりです。

./StructureContents_GET_RenderedContent_ById.sh 43849 43823
Foo 2021年8月30日 - 00:00:00 オプション13142925

cURLスクリプトのパラメータ:

パラメーター #説明
$1構造化コンテンツID
$2コンテンツテンプレートID

または、 StructureContents_GET_RenderedContent_ById Java クラスを実行します。 以下の値を、ご自身の記事IDとテンプレートIDに置き換えてください。

java -classpath .:* -DcontentTemplateId=1234 -DstructuredContentId=5678 StructureContents_GET_RenderedContent_ById

ウェブコンテンツ記事のパッチ適用

PATCH HTTP メソッドを StructuredContent サービスで使用して、元の記事情報を部分的に更新します。 StructuredContents_PATCH_ById スクリプトは、記事と構造の識別子を使用して、記事のタイトルを Charlie に置き換えます。

メソッドサービスエンドポイント
パッチ構造化コンテンツ/v1.0/structured-contents/{structuredContentId}
./StructuredContents_PATCH_ById.sh 43849 43563

cURLスクリプトのパラメータ:

パラメーター #説明
$1構造化コンテンツID
$2contentStructured

または、 StructuredContents_PATCH_ById Java クラスを実行します。 値を記事IDに置き換えてください。

java -classpath .:* -DstructuredContentId=1234 StructuredContents_PATCH_ById

ウェブコンテンツ記事を掲載する

PUT HTTP メソッドを StructuredContent サービスで使用して、元の記事情報を置き換えます。 StructuredContents_PUT_ById スクリプトは、記事と構造の識別子を使用して、記事の名前を Baker に、記事の内容を Bar から Goo に置き換えます。

方法サービス終点
置く構造化コンテンツ/v1.0/structured-contents/{structuredContentId}
./StructuredContents_PUT_ById.sh 43849 43563 43805

cURLスクリプトのパラメータ:

パラメータ番号説明
1ドル構造化コンテンツID
2ドル画像ID
3ドルcontentStructured

または、 StructuredContents_PUT_ById Java クラスを実行します。 以下の値を、記事、画像、構造物のIDに置き換えてください。

java -classpath .:* -DcontentStructureId=1234 -DimageId=5678 -DstructuredContentId=91011 StructuredContents_PUT_ById