WebコンテンツAPIの基本
Liferay DXPのRESTサービスを使用すると、サイト上で構造化コンテンツを作成および管理できます。 構造化コンテンツとは、著者、要約、その他の関連コンテンツ要素など、各記事に必要な特定のフィールドと情報を定義することにより、一貫した形式と構成を強制するために、 Web コンテンツ構造 を使用する Web コンテンツ記事 を指します。
Web コンテンツ テンプレート を使用して構造化コンテンツをレンダリングすることはできますが、構造化コンテンツを作成するためにテンプレートは必須ではありません。
構造と記事については、以下のcURLとJavaのサンプルを参照してください。 構造化コンテンツの管理に関するより高度な例については、 高度な Web コンテンツ API を参照してください。 Liferay DXP で REST API を使用する方法の概要については、 REST サービスの利用 を参照してください。
環境のセットアップ
新しいLiferay インスタンスを起動し、以下を実行します。
docker run -it -m 8g -p 8080:8080 liferay/portal:7.4.3.132-ga132
http://localhost:8080 で Liferay にサインインしてください。 メールアドレス test@liferay.com とパスワード test を使用してください。 プロンプトが表示されたら、パスワードを learnに変更します。
次に、以下の手順に従います。
-
Web Content API の基本 ファイルをダウンロードして解凍します。
curl https://resources.learn.liferay.com/examples/liferay-r4h9.zip -Ounzip liferay-r4h9.zip注これらのスクリプトは基本認証を使用し、テスト用に設計されています。 本番のLiferay DXP環境では、基本認証を使用しないでください。
-
以下の手順で環境をセットアップします。
使用するサービスの特定
Liferay DXP Headless Delivery API の以下のサービスを使用して、Web コンテンツを管理します。
記事用の構造化コンテンツ。構造のためのコンテンツ構造。- テンプレート用の
ContentTemplate。
これらの API の詳細情報を表示するには、ブラウザを使用して Liferay の API Explorer にアクセスしてください。 [server]:[port]/o/api (例: http://localhost:8080/o/api)。 詳細については、 REST サービスの利用 をお読みください。
サイトIDの特定
ログインすると、 はサイト ID を取得します。 このIDを複数のサービス呼び出しで使用してください。 この例では、ID は 20117 です。
ユーザーインターフェースでWebコンテンツ構造を作成する
記事を作成するには、構成が必要です。 ユーザーインターフェースで構造を指定せずに記事を作成すると、Liferay DXPはデフォルトの基本Webコンテンツ構造を使用します。
Liferay DXPのユーザーインターフェースでは、基本Webコンテンツ構造は表示されません。
基本ウェブコンテンツ構造を使用する代わりに、新しい構造を作成します。
-
サイトメニュー (../../../../images/icon-menu.png) を開き、 コンテンツ & データを展開し、 Web コンテンツ に移動します。
-
構造 タブを選択し、 新規をクリックします。
-
名前は Foo 構造 です。 右側のビルダーメニューで、 テキスト フィールドを選択し、フォーム領域にドラッグしてドロップします。
-
右側に開いたコンテキストメニューで、 詳細 タブを選択し、 フィールド参照 を
コンテンツに変更します。 -
[保存]をクリックします。
詳細については、 構造の作成 を参照してください。
ユーザーインターフェースでWebコンテンツ記事を作成する
これで、Foo構造に基づいて記事を作成できます。
-
サイトメニュー (../../../../images/icon-menu.png) を開き、 コンテンツ & データを展開し、 Web コンテンツ に移動します。
-
Webコンテンツ タブで、 新規 をクリックし、 Foo構造 を選択します。
-
新しい名前として Foo Article と入力し、 公開 をクリックします。

詳細については、 Web コンテンツ記事の作成 を参照してください。
サイトからウェブコンテンツ記事を取得する
以下のcURLスクリプトまたはJavaクラスを実行して、REST APIを使用してサイトの記事一覧を取得します。 1234をサイトのIDに置き換えてください。
StructuredContents_GET_FromSites.sh
StructuredContents_GET_FromSites.sh cURL スクリプトは、サイトのすべての記事を一覧表示します。 このスクリプトは、サイトIDを唯一のパラメーターとして、GET HTTPメソッドでStructuredContentサービスを使用します。
| メソッド | サービス | エンドポイント |
|---|---|---|
GET | StructuredContent | /v1.0/sites/{siteID}/structured-contents |
./StructuredContents_GET_FromSites.sh 1234
| パラメーター # | 説明 |
|---|---|
$1 | siteId |
以下は、このスクリプトが生成するJSON出力の一部です。 この出力では、 id で識別される単一の記事と、 friendlyUrlPath のフレンドリー URL を確認できます。 この記事では、 contentStructureId の構造を使用しています。
この構造には、 contentFieldValue セクションの contentFields で説明されている単一のテキスト フィールドがあります。 構造に要素を追加すると、これらの要素を説明する追加の contentFieldValue セクションが表示されます。
{
{
...
"items": [
{
...
"availableLanguages": [
"en-US"
],
"contentFields": [
{
"contentFieldValue": {
"data": ""
},
"dataType": "string",
"inputControl": "text",
"label": "Text",
"name": "content",
"nestedContentFields": [],
"repeatable": false
}
],
"contentStructureId": 32122,
"creator": {
"additionalName": "",
"contentType": "UserAccount",
"familyName": "Administrator",
"givenName": "Master",
"id": 20123,
"name": "Master Administrator"
},
"customFields": [],
"dateCreated": "2024-10-02T11:29:20Z",
"dateModified": "2024-10-02T11:29:20Z",
"datePublished": "2024-10-02T11:29:00Z",
"description": "",
"externalReferenceCode": "0b1a40b2-687e-c88a-f8d9-5270ec94fdd6",
"friendlyUrlPath": "foo-article",
"id": 32147,
"key": "32145",
"keywords": [],
"neverExpire": true,
"numberOfComments": 0,
"priority": 0,
"relatedContents": [],
"renderedContents": [],
"siteId": 20117,
"structuredContentFolderId": 0,
"subscribed": false,
"taxonomyCategoryBriefs": [],
"title": "Foo Article",
"uuid": "0b1a40b2-687e-c88a-f8d9-5270ec94fdd6"
}
],
"lastPage": 1,
"page": 1,
"pageSize": 20,
"totalCount": 1
}
JSON出力で以下の情報を確認します。
-
出力には、
id: 32147、title: Foo Article、フレンドリー URLfriendlyUrlPath: foo-articleで識別される単一の記事が表示されます。 -
この記事では、
contentStructureIdで識別される Foo 構造を使用します。 -
この構造には、
contentFieldValueセクションのcontentFieldsで説明されている単一のテキスト フィールドがあります。 構造に要素を追加すると、これらの要素を説明する追加のcontentFieldValueセクションが表示されます。 -
ユーザーインターフェースに表示される
IDの値は、JSON 出力のキープロパティに対応します。
StructuredContents_GET_FromSites.java
StructuredContents_GET_FromSites.java クラスは、 StructuredContentResource サービスを呼び出すことで記事のリストを取得します。
public static void main(String[] args) throws Exception {
StructuredContentResource.Builder builder =
StructuredContentResource.builder();
StructuredContentResource structuredContentResource =
builder.authentication(
"test@liferay.com", "learn"
).build();
Page<StructuredContent> page =
structuredContentResource.getSiteStructuredContentsPage(
Long.valueOf(System.getProperty("siteId")), null, null, null,
null, Pagination.of(1, 2), null);
System.out.println(page.getItems());
}
このクラスは、わずか3行のコードでRESTサービスを呼び出します。
| 行(省略形) | 説明 |
|---|---|
StructuredContentResource.Builder builder = ... | Builderを取得し、StructuredContentResourceサービスインスタンスを生成します。 |
StructuredContentResource structuredContentResource = builder.authentication(...).build(); | 基本認証を指定し、StructuredContentResource サービスインスタンスを生成します。 |
Page<StructuredContent> page = structuredContentResource.getSiteStructuredContentsPage(...); | structuredContentResource.getSiteStructuredContentsPageメソッドを呼び出し、データを取得します。 |
プロジェクトには、依存関係としてcom.liferay.headless.delivery.client.jarファイルが含まれていることに注意してください。 すべてのRESTアプリケーションのクライアントJAR依存関係情報は、/o/apiでインストール先のAPIエクスプローラーで確認できます。
main メソッドのコメントは、クラスを実行する方法を示しています。
他の例のJavaクラスはこれと類似していますが、異なるStructuredContentResourceメソッドを呼び出します。
サービスの詳細については、 StructuredContentResource を参照してください。
Webコンテンツ記事の取得
前のステップのスクリプトはサイトの全ての記事を返しますが、StructuredContents_GET_ById.[java|sh]スクリプト/クラスを使い、記事のIDを指定することで特定の記事を取得することができます。
コマンド:
[articleId] と -DstructuredContentId=1234 の値を記事のIDに置き換えてください。
./StructuredContents_GET_ById.sh [articleId]
java -classpath .:* -DstructuredContentId=1234 StructuredContents_GET_ById
StructuredContentフィールドはJSONで表示されます。
サイトからウェブコンテンツ構造を取得する
以下のcURLスクリプトまたはJavaクラスを実行して、REST APIを使用してサイトの構造リストを取得します。 [siteId] と -DsiteId=1234 の値をサイト ID に置き換えます。
このエンドポイントでは、デフォルトの基本Webコンテンツ構造は表示されません。
コマンド:
./ContentStructures_GET_FromSite.sh [siteId]
java -classpath .:* -DsiteId=1234 ContentStructures_GET_FromSite
サイトの ContentStructures は JSON で表示されます。 基本ウェブコンテンツ構造は表示されていないため、レスポンスにも表示されません。
{
"actions" : { },
"facets" : [ ],
"items" : [ {
"availableLanguages" : [ "en-US" ],
"contentStructureFields" : [ {
"dataType" : "string",
"inputControl" : "text",
"label" : "Text",
"localizable" : true,
"multiple" : false,
"name" : "content",
"nestedContentStructureFields" : [ ],
"options" : [ ],
"predefinedValue" : "",
"repeatable" : false,
"required" : false,
"showLabel" : true
} ],
"creator" : {
"additionalName" : "",
"contentType" : "UserAccount",
"familyName" : "Administrator",
"givenName" : "Master",
"id" : 20123,
"name" : "Master Administrator"
},
"dateCreated" : "2024-10-01T18:34:15Z",
"dateModified" : "2024-10-01T18:34:15Z",
"description" : "",
"id" : 32122,
"name" : "Foo Structure",
"siteId" : 20117
} ],
"lastPage" : 1,
"page" : 1,
"pageSize" : 20,
"totalCount" : 1
}
構造の作成者 、その ID、 名前、および構造で使用されているフィールドに関する情報を見つけることができる contentStructureFields の詳細を確認できます。
ウェブコンテンツ構造を取得する
パラメータとしてIDを指定することで、 ContentStructure サービスを使用して、特定の構造の説明を取得します。
ContentStructures_GET_ById.sh
| メソッド | サービス | エンドポイント |
|---|---|---|
GET | ContentStructure | /v1.0/content-structures/{contentStructureId} |
./ContentStructures_GET_ById.sh 1234
| パラメーター # | 説明 |
|---|---|
| $1 | contentStructureId |
以下は、JSON出力です。 構造の ID と 名前 を識別できます。 contentStructureFields セクションには、構造フィールドの説明が含まれています。 この構造には、タイプ 文字列 で名前 Content の単一のコンテンツ フィールドが含まれていることに注意してください。
{
"availableLanguages" : [ "en-US" ],
"contentStructureFields" : [ {
"dataType" : "string",
"inputControl" : "text",
"label" : "Text",
"localizable" : true,
"multiple" : false,
"name" : "content",
"nestedContentStructureFields" : [ ],
"options" : [ ],
"predefinedValue" : "",
"repeatable" : false,
"required" : false,
"showLabel" : true
} ],
"creator" : {
"additionalName" : "",
"contentType" : "UserAccount",
"familyName" : "Administrator",
"givenName" : "Master",
"id" : 20123,
"name" : "Master Administrator"
},
"dateCreated" : "2024-10-02T10:50:55Z",
"dateModified" : "2024-10-02T10:50:55Z",
"description" : "",
"id" : 33862,
"name" : "Foo Structure",
"siteId" : 20117
}
同様に、 ContentStructures_GET_ById Java クラスを実行することもできます。
java -classpath .:* -DcontentStructureId=1234 ContentStructures_GET_ById
ContentStructure の説明は JSON で表示されます。
ウェブコンテンツ記事の投稿
StructuredContents_POST_ToSite[java|sh] スクリプト/クラスを使用して、Foo 構造を使用して新しい記事を作成します。
StructuredContents_POST_ToSite.sh
StructuredContents_POST_ToSite.sh cURL スクリプトは、 POST HTTP メソッドと Foo 構造を使用して新しい記事を作成します。 スクリプトは、 サイト ID と構造 ID をパラメータとして使用します。
| メソッド | サービス | エンドポイント |
|---|---|---|
PUT | StructuredContent | /v1.0/sites/{siteId}/structured-contents |
./StructuredContents_POST_ToSite.sh [siteId] [structureId]
cURLスクリプトのパラメーター:
| パラメーター # | 説明 |
|---|---|
| $1 | siteId |
| $2 | contentStructureId |
Liferay DXP で新しい記事を見つけるには、 サイト メニュー (
) を開き、 コンテンツ & データ → Web コンテンツ に移動します。
以下は、このスクリプトが生成するJSON出力の一部です。 スクリプトは、構造contentField の 内容を参照として、単純な<p>Goo</p>HTML 文字列を投稿します。
{
...
"availableLanguages" : [ "en-US" ],
"contentFields" : [ {
"contentFieldValue" : {
"data" : "<p>Goo</p>"
},
"dataType" : "string",
"inputControl" : "text",
"label" : "Text",
"name" : "content",
"nestedContentFields" : [ ],
"repeatable" : false
} ],
"contentStructureId" : 32122,
"creator" : {
"additionalName" : "",
"contentType" : "UserAccount",
"familyName" : "Administrator",
"givenName" : "Master",
"id" : 20123,
"name" : "Master Administrator"
},
"customFields" : [ ],
"dateCreated" : "2024-10-02T11:57:26Z",
"dateModified" : "2024-10-02T11:57:26Z",
"datePublished" : "2024-10-02T11:57:00Z",
"description" : "",
"externalReferenceCode" : "009a771b-2e71-de18-83e2-6ae165ce93c5",
"friendlyUrlPath" : "goo-article",
"id" : 32177,
"key" : "32175",
"keywords" : [ ],
"neverExpire" : true,
"numberOfComments" : 0,
"priority" : 0.0,
"relatedContents" : [ ],
"renderedContents" : [ ],
"siteId" : 20117,
"structuredContentFolderId" : 0,
"subscribed" : false,
"taxonomyCategoryBriefs" : [ ],
"title" : "Goo Article",
"uuid" : "009a771b-2e71-de18-83e2-6ae165ce93c5"
}
同様に、 StructuredContents_POST_ToSite.java クラスは、 StructuredContentResource サービスを呼び出すことで記事を追加します。
コマンド:
-DcontentStructureId=1234 および -DsiteId=5678 の値を、それぞれ構造の ID とサイトの ID に置き換えてください。
java -classpath .:* -DcontentStructureId=1234 -DsiteId=5678 StructuredContents_POST_ToSite
StructuredContentフィールドはJSONで表示されます。
ウェブコンテンツ記事を修正する
PATCH メソッドを StructuredContent サービスで使用して記事を更新します。 StructuredContent_PATCH_ById.[java|sh] スクリプト/クラスは、構造化コンテンツ識別子 id を使用して、記事のコンテンツを ‘Goo' から ‘Foo' に更新します。
コマンド:
./StructuredContent_PATCH_ById.sh [articleId]
-DcontentStructureId=1234 および -DstructuredContentId=5678 の値を、それぞれ構造の ID と記事の ID に置き換えてください。
java -classpath .:* -DcontentStructureId=1234 -DstructuredContentId=5678 StructuredContents_PATCH_ById
ウェブコンテンツ記事を掲載する
PUT メソッドを StructuredContent サービスで使用して、元の記事の情報を置き換えます。 StructuredContents_PUT_ById.[java|sh] スクリプト/クラスは、記事および構造識別子を使用して、記事の名前と記事の内容を Foo から Bar に置き換えます。
コマンド:
./StructuredContents_PUT_ById.sh [articleId] [structureId]
-DcontentStructureId=1234 および -DstructuredContentId=5678 の値を、それぞれ構造の ID と記事の ID に置き換えてください。
java -classpath .:* -DcontentStructureId=1234 -DstructuredContentId=5678 StructuredContents_PUT_ById
ウェブコンテンツ記事の削除
DELETE メソッドを StructuredContent サービスで使用して記事を削除します。 StructuredContents_DELETE_ById.[java|sh] スクリプト/クラスの例では、記事の ID を使用して記事を削除します。
REST API を使用して記事を削除すると、Liferay DXP のごみ箱を使用せずに永久に削除されます。
コマンド:
./StructuredContents_DELETE_ById.sh [articleId]
-DstructuredContentId=1234 の値を記事の ID に置き換えてください。
java -classpath .:* -DstructuredContentId=1234 StructuredContents_DELETE_ById
その他のWebコンテンツとWebコンテンツフォルダーのサービス
他のcURLコマンドとJavaクラスは、その他の StructuredContentとStructuredContentFolder サービスの詳細を示しています。 これらは Web Content API の基本 で見つけることができます。
| ファイル | 説明 |
|---|---|
StructuredContentFolders_GET_ById.[java\|sh] | ウェブコンテンツフォルダのフィールドを一覧表示します。 |
StructuredContentFolders_GET_FromSites.[java\|sh] | サイト内のすべてのウェブコンテンツフォルダを一覧表示します。 |
StructuredContentFolders_POST_ToSites.[java\|sh] | ウェブコンテンツフォルダをサイトに投稿します。 |
StructuredContentFolders_PATCH_ById.[java\|sh] | ウェブコンテンツフォルダを更新します。 |
StructuredContentFolders_PUT_ById.[java\|sh] | ウェブコンテンツフォルダを置き換えます。 |
StructuredContentFolders_DELETE_ById.[java\|sh] | ウェブコンテンツフォルダを削除します。 |
StructuredContents_POST_ToStructuredContentFolders.[java\|sh] | ウェブコンテンツ記事をフォルダに投稿します。 |
REST API を使用して Web コンテンツ フォルダーを削除すると、フォルダーとそのコンテンツは、 Liferay DXP ごみ箱 を使用せずに完全に削除されます。