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 コンテンツ API の基礎 ファイルをダウンロードして解凍します。
curl https://resources.learn.liferay.com/examples/liferay-r4h9.zip -Ounzip liferay-r4h9.zip注これらのスクリプトは基本認証を使用し、テスト用に設計されています。 本番のLiferay DXP環境では、基本認証を使用しないでください。
-
以下の手順で環境をセットアップします。
使用するサービスの特定
Liferay DXP ヘッドレス配信 API の次のサービスを使用して、Web コンテンツを管理します。
記事の StructuredContent。構造体の ContentStructure。テンプレートの ContentTemplate。
これらの API の詳細情報を表示するには、ブラウザを使用して、 [server]:[port]/o/api (例: http://localhost:8080/o/api) にある Liferay の API Explorer にアクセスします。 詳細については、「 REST サービスの使用」をお読みください。
サイトIDの特定
サインインすると、 サイト IDを取得します。 この ID を複数のサービス呼び出しで使用します。 この例では、IDは 20117です。
ユーザーインターフェースでWebコンテンツ構造を作成する
記事を作成するには、構造が必要です。 構造を宣言せずにユーザーインターフェースで記事を作成すると、Liferay DXP はデフォルトの基本 Web コンテンツ構造を使用します。
基本的な Web コンテンツ構造は、Liferay DXP ユーザー インターフェイスには表示されません。
基本的な Web コンテンツ構造を使用する代わりに、新しいものを作成します。
-
サイト メニュー (../../../../images/icon-menu.png) を開き、 コンテンツ & データを展開して、 Web コンテンツに移動します。
-
構造 タブを選択し、 新規をクリックします。
-
名前を Foo Structureにします。 右側のビルダー メニューで、 テキスト フィールドを選択し、フォーム領域にドラッグ アンド ドロップします。
-
右側に表示されるコンテキスト メニューで、 詳細設定 タブを選択し、 フィールド参照 を
コンテンツに変更します。 -
[保存]をクリックします。
詳細については、「 構造の作成」を参照してください。
ユーザーインターフェースでWebコンテンツ記事を作成する
これで、Foo 構造に基づいて記事を作成できます。
-
サイト メニュー (../../../../images/icon-menu.png) を開き、 コンテンツ & データを展開して、 Web コンテンツに移動します。
-
Web コンテンツ タブで、 新規 をクリックし、 Foo 構造を選択します。
-
新しい名前として Foo Article を入力し、 公開をクリックします。

詳細については、「 Web コンテンツ記事の作成」を参照してください。
サイトからWebコンテンツ記事を取得する
次の cURL スクリプトまたは Java クラスを実行して、REST API でサイトの記事のリストを取得します。 1234をサイトのIDに置き換えてください。
構造化コンテンツ_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 出力のキープロパティに対応します。
構造化コンテンツ_GET_FromSites.java
StructuredContents_GET_FromSites.java クラスは、構造化コンテンツ関連のサービスを呼び出して記事のリストを取得します。
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エクスプローラーで確認できます。
メイン メソッドのコメントは、クラスの実行を示しています。
他の例の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で表示されます。
サイトからWebコンテンツ構造を取得する
REST API を使用して、次の cURL スクリプトまたは Java クラスを実行し、サイトの構造のリストを取得します。 [siteId] と -DsiteId=1234 の値をサイト ID に置き換えます。
このエンドポイントを使用すると、デフォルトの基本 Web コンテンツ構造は表示されません。
コマンド:
./ContentStructures_GET_FromSite.sh [siteId]
java -classpath .:* -DsiteId=1234 ContentStructures_GET_FromSite
サイトの ContentStructures は JSON 形式で表示されます。 基本的な Web コンテンツの構造は表示されないため、応答には表示されません。
{
"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 に関する詳細が表示され、構造体で使用されているフィールドに関する情報を見つけることができます。
Webコンテンツの構造を取得する
特定の構造体の ID をパラメータとして指定してその構造体の説明を取得するには、 ContentStructure サービスを使用します。
コンテンツ構造_GET_ById.sh
| メソッド | サービス | エンドポイント |
|---|---|---|
GET | ContentStructure | /v1.0/content-structures/{contentStructureId} |
./ContentStructures_GET_ById.sh 1234
| パラメーター # | 説明 |
|---|---|
| $1 | contentStructureId |
以下は、JSON出力です。 構造体の ID と 名前を識別できます。 contentStructureFields セクションには、構造体フィールドの説明が含まれています。 この構造体には、タイプ 文字列 および名前 コンテンツの単一のコンテンツ フィールドが含まれていることに注意してください。
{
"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 で表示されます。
Webコンテンツ記事の投稿
Foo 構造を使用して新しい記事を作成するには、 StructuredContents_POST_ToSite[java|sh] スクリプト/クラスを使用します。
構造化コンテンツ_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出力の一部です。 このスクリプトは、Structure contentField content を参照として使用し、単純な <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 クラスは、構造化コンテンツ関連のサービスを呼び出して記事を追加します。
コマンド:
-DcontentStructureId=1234 と -DsiteId=5678 の値を、それぞれ構造とサイトの ID に置き換えます。
java -classpath .:* -DcontentStructureId=1234 -DsiteId=5678 StructuredContents_POST_ToSite
StructuredContentフィールドはJSONで表示されます。
Webコンテンツ記事のパッチ
記事を更新するには、 PATCH メソッドを StructuredContent サービスと共に使用します。 StructuredContent_PATCH_ById.[java|sh] スクリプト/クラスは、構造化コンテンツ識別子 id を使用して、記事のコンテンツを ‘Goo' から ‘Foo' に更新します。
コマンド:
./StructuredContent_PATCH_ById.sh [articleId]
-DcontentStructureId=1234 と -DstructuredContentId=5678 の値を、それぞれ構造と記事の 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 に置き換えます。
java -classpath .:* -DcontentStructureId=1234 -DstructuredContentId=5678 StructuredContents_PUT_ById
Webコンテンツ記事の削除
記事を削除するには、 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 コンテンツ API の基礎に記載されています。
| ファイル | 説明 |
|---|---|
StructuredContentFolders_GET_ById.[java\|sh] | Web コンテンツ フォルダーのフィールドを一覧表示します。 |
StructuredContentFolders_GET_FromSites.[java\|sh] | サイト内のすべての Web コンテンツ フォルダーを一覧表示します。 |
StructuredContentFolders_POST_ToSites.[java\|sh] | Web コンテンツ フォルダーをサイトに投稿します。 |
StructuredContentFolders_PATCH_ById.[java\|sh] | Web コンテンツ フォルダーを更新します。 |
StructuredContentFolders_PUT_ById.[java\|sh] | Web コンテンツ フォルダーを置き換えます。 |
StructuredContentFolders_DELETE_ById.[java\|sh] | Web コンテンツ フォルダーを削除します。 |
StructuredContents_POST_ToStructuredContentFolders.[java\|sh] | Web コンテンツの記事をフォルダーに投稿します。 |
REST API を使用して Web コンテンツ フォルダを削除すると、 Liferay DXP ごみ箱を使用せずに、フォルダとそのコンテンツが完全に削除されます。