oo

WebコンテンツAPIの基本

Liferay DXP RESTサービスを使用すると、サイトで構造化コンテンツを作成・管理することができます。 構造化コンテンツとは、Webコンテンツストラクチャーを使用するWebコンテンツ です。 ストラクチャーは、Webコンテンツの記事に含まれる作成者、概要、内容などの情報を定義します。 ストラクチャーにより、必要な情報がコンテンツにすべて含まれるようになります。 詳細については、 Webコンテンツストラクチャーを理解するをご覧ください。

Note

構造化コンテンツをレンダリングするために、Webコンテンツテンプレートでストラクチャーを使用できますが、構造化コンテンツの作成にテンプレートは必要ありません。

ストラクチャーや構造化コンテンツのcURLとJavaのサンプルについては以下を参照してください。 構造化コンテンツの管理に関するより詳細な例については、 WebコンテンツAPIの詳細設定を参照してください。 Liferay DXPでのREST APIの使用については、 RESTサービスの使用をご覧ください。

環境のセットアップ

新しいLiferay インスタンスを起動し、以下を実行します。

docker run -it -m 8g -p 8080:8080 liferay/portal:7.4.3.112-ga112。

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

次に、以下の手順を実行します。

  1. Web Content API Basics ファイルをダウンロードし、解凍します:

    curl https://resources.learn.liferay.com/dxp/latest/en/content-authoring-and-management/web-content/developer-guide/liferay-r4h9.zip -O
    
    unzip liferay-r4h9.zip
    
    Note

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

  2. 以下の手順で環境をセットアップします。

    1. 使用するサービスの特定
    2. サイトIDを特定します。

使用するサービスの特定

Webコンテンツの管理には、Liferay DXP Headless Delivery API で下記のサービスを使用します。

  • Webコンテンツ記事用のStructuredContent
  • Webコンテンツストラクチャー用のContentStructure
  • Webコンテンツテンプレート用のContentTemplate

このサービスと利用可能なHTTPメソッドを識別するには、Liferay APIエクスプローラーを使用します。 詳しくは、RESTサービスの使用を参照してください。

サイトIDの特定

サイトIDの特定は、 こちらの の説明に従ってください。

ユーザーインターフェースでの基本Webコンテンツ記事の作成

Webコンテンツを作成するには、Webコンテンツストラクチャーが必要です。 ユーザーインターフェースでストラクチャーを宣言せずにWebコンテンツを作成した場合、Liferay DXPでは、デフォルトの基本Webコンテンツストラクチャーを使用します。

Note

基本Webコンテンツストラクチャーは、Liferay DXPのユーザーインターフェースには表示されません。

これらの例では、単一のテキストフィールドとデフォルトの基本Webコンテンツストラクチャーを持つ基本Webコンテンツ記事を使用しています。 以下の手順で、Webコンテンツを作成します。

  1. サイトメニュー(Site menu)を開き、 [Content & Data] → [Web Content] に移動します。

  2. Web Content] タブで、追加 (Add)をクリックし、 [Basic Web Content] を選択します。

    Webコンテンツパネルから基本Webコンテンツ記事を作成する。

  3. 新しいWebコンテンツの名前として Foo を入力し、 [Publish] をクリックします。

    Webコンテンツ記事は、デフォルトで基本Webコンテンツストラクチャーを使用します。

詳しくは、 基本Webコンテンツ記事の追加を参照してください。

Webコンテンツ記事のサイトからの取得

次のcURLまたはJavaコマンドを実行すると、サイトのWebコンテンツ記事を取得できます。 1234 をサイトのIDに置き換えてください。

StructuredContents_GET_FromSite.sh

StructuredContents_GET_FromSite.sh cURLスクリプトは、サイトのすべてのWebコンテンツ記事を一覧表示します。 このスクリプトは、サイトIDを唯一のパラメーターとして、GET HTTPメソッドでStructuredContentサービスを使用します。

メソッド サービス エンドポイント
GET StructuredContent /v1.0/sites/{siteID}/structured-contents
./StructuredContents_GET_FromSite.sh 1234
パラメーター # 説明
$1 siteId

以下は、このスクリプトが生成するJSON出力の一部です。 この出力では、idfriendlyUrlPathのフレンドリーURLによって識別される単一のWebコンテンツ記事が表示されます。 Webコンテンツは、contentStructureIdのストラクチャーを使用します。 ストラクチャーには、contentFieldscontentFieldValueセクションで説明される単一のテキストフィールドがあります。 ストラクチャーにさらに多くの要素を含めると、これらの要素を説明するcontentFieldValueセクションが追加で表示されます。

{
  "actions" : {
    ...
  },
  "facets" : [ ],
  "items" : [ {
    "actions" : {
   ...
    },
    "availableLanguages" : [ "en-US" ],
    "contentFields" : [ {
      "contentFieldValue" : {
        "data" : ""
      },
      "dataType" : "string",
      "label" : "content",
      "name" : "content",
      "nestedContentFields" : [ ],
      "repeatable" : false
    } ],
    "contentStructureId" : 40697,
    "creator" : {
      "additionalName" : "",
      "contentType" : "UserAccount",
      "familyName" : "Bowman",
      "givenName" : "David",
      "id" : 20129,
      "name" : "David Bowman"
    },
    "customFields" : [ ],
    "dateCreated" : "2021-08-10T08:10:21Z",
    "dateModified" : "2021-08-10T08:10:21Z",
    "datePublished" : "2021-08-10T08:10:00Z",
    "description" : "",
    "externalReferenceCode" : "41537",
    "friendlyUrlPath" : "foo",
    "id" : 41539,
    "key" : "41537",
    "keywords" : [ ],
    "numberOfComments" : 0,
    "relatedContents" : [ ],
    "renderedContents" : [ {
      "contentTemplateId" : "BASIC-WEB-CONTENT",
      "contentTemplateName" : "Basic Web Content",
      "markedAsDefault" : true,
      "renderedContentURL" : "http://localhost:8080/o/headless-delivery/v1.0/structured-contents/41539/rendered-content/BASIC-WEB-CONTENT"
    } ],
    "siteId" : 20125,
    "subscribed" : false,
    "taxonomyCategoryBriefs" : [ ],
    "title" : "Foo",
    "uuid" : "162155dc-c9aa-96b0-df5c-a61c591d1389"
  } ],
  "lastPage" : 1,
  "page" : 1,
  "pageSize" : 20,
  "totalCount" : 1
}

JSON出力で以下の情報を確認します。

  • 出力には、id: 41539title: Foo、そしてフレンドリーURL friendlyUrlPath: fooで識別される単一の Webコンテンツ記事が表示されます。

  • このWebコンテンツは、contentStructureIdで特定されるデフォルトのLiferay DXP Webコンテンツストラクチャーを使用します。

  • Webコンテンツストラクチャーには、contentFieldscontentFieldValueセクションで説明される単一のテキストフィールドがあります。 ストラクチャーにさらに多くの要素を含めると、これらの要素を説明するcontentFieldValueセクションが追加で表示されます。

  • ユーザーインターフェースのWebコンテンツIDは、JSON出力のキープロパティに対応します。

    JSONキープロパティは、ユーザーインターフェースにおける構造化コンテンツ識別子に相当します。

StructuredContents_GET_FromSite.java

StructuredContents_GET_FromSite.javaクラスは、構造化コンテンツ関連サービスを呼び出してWebコンテンツの記事一覧を取得します。

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);
}

このクラスは、わずか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エクスプローラーで確認できます。

Note

mainメソッドのコメントでは、クラスの実行を実演しています。

他の例のJavaクラスはこれと類似していますが、異なるStructuredContentResourceメソッドを呼び出します。

Important

サービスの詳細は、 StructuredContentResource を参照してください。

以下は、cURLとJavaを使って、他のRESTサービスを呼び出す例です。

Webコンテンツ記事の取得

前の手順の スクリプトは、サイトのすべてのWebコンテンツ記事を返します。 特定の記事を取得するには、 StructuredContent_GET_ById.[java|sh]スクリプトを使用します。 1234をWebコンテンツ記事のIDに置き換えてください。

StructuredContent_GET_ById.sh

コマンド:

./StructuredContent_GET_ById.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-delivery/v1.0/structured-contents/${1}" \
	--user "test@liferay.com:learn"

StructuredContent_GET_ById.java

コマンド:

java -classpath .:* -DstructuredContentId=1234 StructuredContent_GET_ById

コード:

public static void main(String[] args) throws Exception {
	StructuredContentResource.Builder builder =
		StructuredContentResource.builder();

	StructuredContentResource structuredContentResource =
		builder.authentication(
			"test@liferay.com", "learn"
		).build();

	System.out.println(
		structuredContentResource.getStructuredContent(
			Long.valueOf(System.getProperty("structuredContentId"))));
}

StructuredContentフィールドはJSONで表示されます。

Webコンテンツストラクチャーの取得

次のcURLまたはJavaコマンドを実行すると、サイトのコンテンツストラクチャーを取得できます。 1234をサイトのIDに置き換えてください。

Note

デフォルトの基本Webコンテンツストラクチャーは、このエンドポイントを使用すると表示されません。

ContentStructures_GET_FromSite.sh

コマンド:

./ContentStructures_GET_FromSite.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-delivery/v1.0/sites/${1}/content-structures" \
	--user "test@liferay.com:learn"

ContentStructures_GET_FromSite.java

コマンド:

java -classpath .:* -DsiteId=1234 ContentStructures_GET_FromSite

コード:

public static void main(String[] args) throws Exception {
	ContentStructureResource.Builder builder =
		ContentStructureResource.builder();

	ContentStructureResource contentStructureResource =
		builder.authentication(
			"test@liferay.com", "learn"
		).build();

	Page<ContentStructure> page =
		contentStructureResource.getSiteContentStructuresPage(
			Long.valueOf(System.getProperty("siteId")), null, null, null,
			Pagination.of(1, 2), null);

	System.out.println(page);
}

サイトのContentStructureオブジェクトは、JSONで表示されます。

IDによるWebコンテンツストラクチャーの取得

サンプルのWebコンテンツ記事にあるデフォルトのWebコンテンツストラクチャーは、Liferay DXPのユーザーインターフェースでは表示されません。 しかし、ContentStructureサービスを使用すれば、ストラクチャーの説明を収集することができます。

ContentStructure_GET_ById.[java|sh]スクリプトは、Webコンテンツストラクチャーの説明を返します。

ContentStructure_GET_ById.sh

メソッド サービス エンドポイント
GET ContentStructure /v1.0/content-structures/{contentStructureId}
./ContentStructure_GET_ById.sh 40697
パラメーター # 説明
$1 contentStructureId

以下は、JSON出力です。 Liferay DXPのデフォルトのWebコンテンツストラクチャーは、 idnameで識別できます。 contentStructureFieldsセクションには、ストラクチャー項目の説明が含まれています。 このストラクチャーには、タイプ string と名前 contentの単一のコンテンツ項目が含まれていることに注目してください。 基本Webコンテンツの記事の投稿 では、このコンテンツ項目に情報を追加して新規Webコンテンツ記事を作成します。

{
  "availableLanguages" : [ "en-US" ],
  "contentStructureFields" : [ {
    "dataType" : "string",
    "label" : "content",
    "localizable" : true,
    "multiple" : false,
    "name" : "content",
    "nestedContentStructureFields" : [ ],
    "options" : [ ],
    "repeatable" : false,
    "required" : false,
    "showLabel" : true
  } ],
  "dateCreated" : "2021-08-09T23:30:23Z",
  "dateModified" : "2021-08-09T23:30:23Z",
  "description" : "Basic Web Content",
  "id" : 40697,
  "name" : "Basic Web Content",
  "siteId" : 20127
}

ContentStructure_GET_ById.java

コマンド:

java -classpath .:* -DcontentStructureId=1234 ContentStructure_GET_ById

コード:

public static void main(String[] args) throws Exception {
	ContentStructureResource.Builder builder =
		ContentStructureResource.builder();

	ContentStructureResource contentStructureResource =
		builder.authentication(
			"test@liferay.com", "learn"
		).build();

	System.out.println(
		contentStructureResource.getContentStructure(
			Long.valueOf(System.getProperty("contentStructureId"))));
}

ContentStructureフィールドは、JSONで表示されます。

基本的なWebコンテンツの記事の投稿

StructuredContent_POST_ToSite[java|sh]スクリプトを使って、新規Webコンテンツ記事を作成します。 1234をWebコンテンツストラクチャーIDに置き換えてください。 5678をサイトのIDに置き換えてください。

StructuredContent_POST_ToSite.sh

StructuredContent_POST_ToSite.shcURLスクリプトサンプルは、POSTHTTPメソッドとデフォルトのWebコンテンツストラクチャーを使って新規Webコンテンツの記事を作成します。 このスクリプトでは、 サイトID とストラクチャーIDをパラメーターとして使用しています。

メソッド サービス エンドポイント
PUT StructuredContent /v1.0/sites/{siteId}/structured-contents
./StructuredContent_POST_ToSite.sh 20125 40697

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

パラメーター # 説明
$1 siteId
$2 contentStructureId

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

以下は、このスクリプトが生成するJSON出力の一部です。 このスクリプトは、ストラクチャーcontentField contentを参照として使用し、シンプルな <p>Foo</p> HTMLストリングを投稿します。

{
  "actions" : {
    ...
  },
  "availableLanguages" : [ "en-US" ],
  "contentFields" : [ {
    "contentFieldValue" : {
      "data" : "<p>Foo</p>"
    },
    "dataType" : "string",
    "label" : "content",
    "name" : "content",
    "nestedContentFields" : [ ],
    "repeatable" : false
  } ],
  "contentStructureId" : 40697,
  "creator" : {
    "additionalName" : "",
    "contentType" : "UserAccount",
    "familyName" : "Bowman",
    "givenName" : "David",
    "id" : 20129,
    "name" : "David Bowman"
  },
  "customFields" : [ ],
  "dateCreated" : "2021-08-10T09:19:40Z",
  "dateModified" : "2021-08-10T09:19:40Z",
  "datePublished" : "2021-08-10T09:19:00Z",
  "description" : "",
  "externalReferenceCode" : "41569",
  "friendlyUrlPath" : "able-article",
  "id" : 41571,
  "key" : "41569",
  "keywords" : [ ],
  "numberOfComments" : 0,
  "relatedContents" : [ ],
  "renderedContents" : [ {
    "contentTemplateId" : "BASIC-WEB-CONTENT",
    "contentTemplateName" : "Basic Web Content",
    "markedAsDefault" : true,
    "renderedContentURL" : "http://localhost:8080/o/headless-delivery/v1.0/structured-contents/41571/rendered-content/BASIC-WEB-CONTENT"
  } ],
  "siteId" : 20125,
  "subscribed" : false,
  "taxonomyCategoryBriefs" : [ ],
  "title" : "Able Article",
  "uuid" : "c7005ffb-5677-e030-9eb9-9b1a24a85054"
}

StructuredContent_POST_ToSite.java

StructuredContent_POST_ToSite.javaクラスは、サービスに関連した構造化コンテンツを呼び出して、Webコンテンツ記事を追加します。

コマンド:

java -classpath .:* -DcontentStructureId=1234 -DsiteId=5678 StructuredContent_POST_ToSite

コード:

public static void main(String[] args) throws Exception {
	StructuredContentResource.Builder builder =
		StructuredContentResource.builder();

	StructuredContentResource structuredContentResource =
		builder.authentication(
			"test@liferay.com", "learn"
		).build();

	StructuredContent structuredContent =
		structuredContentResource.postSiteStructuredContent(
			Long.valueOf(System.getProperty("siteId")),
			new StructuredContent() {
				{
					contentFields = new ContentField[] {
						new ContentField() {
							{
								contentFieldValue =
									new ContentFieldValue() {
										{
											data = "<p>Foo</p>";
										}
									};
								name = "content";
							}
						}
					};
					contentStructureId = Long.valueOf(
						System.getProperty("contentStructureId"));
					title = "Charlie Article";
				}
			});

	System.out.println(structuredContent);
}

StructuredContentフィールドはJSONで表示されます。

Webコンテンツ記事のパッチ適用

StructuredContentサービスでPATCHメソッドを使用して、Webコンテンツ記事を更新します。 StructuredContent_PATCH_ById.[java|sh]スクリプトは構造化コンテンツ識別子 idを使用して、記事のコンテンツを’Foo’から’Bar’に更新します。

StructuredContent_PATCH_ById.sh

コマンド:

./StructuredContent_PATCH_ById.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-delivery/v1.0/structured-contents/${1}" \
	--data-raw '
		{
			"contentFields": [
				{
				"contentFieldValue": {
					"data": "<p>Bar</p>"
				},
				"name": "content"
				}
			]
		}' \
	--header "Content-Type: application/json" \
	--request "PATCH" \
	--user "test@liferay.com:learn"

StructuredContent_PATCH_ById.java

コマンド:

java -classpath .:* -DcontentStructureId=1234 -DstructuredContentId=5678 StructuredContent_PATCH_ById

コード:

public static void main(String[] args) throws Exception {
	StructuredContentResource.Builder builder =
		StructuredContentResource.builder();

	StructuredContentResource structuredContentResource =
		builder.authentication(
			"test@liferay.com", "learn"
		).build();

	StructuredContent structuredContent =
		structuredContentResource.patchStructuredContent(
			Long.valueOf(System.getProperty("structuredContentId")),
			new StructuredContent() {
				{
					contentFields = new ContentField[] {
						new ContentField() {
							{
								contentFieldValue =
									new ContentFieldValue() {
										{
											data = "<p>Bar</p>";
										}
									};
								name = "content";
							}
						}
					};
					contentStructureId = Long.valueOf(
						System.getProperty("contentStructureId"));
				}
			});

	System.out.println(structuredContent);
}

Webコンテンツ記事の配置

StructuredContentサービスと共にPUTHTTPメソッドを使って、オリジナルのWebコンテンツ情報を置き換えることができます。 StructuredContent_PUT_ById.[java|sh]スクリプトはWebコンテンツやストラクチャー識別子を使用して、記事のコンテンツをBarからGooへ更新します。

StructuredContent_PUT_ById.sh

コマンド:

./StructuredContent_PUT_ById.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-delivery/v1.0/structured-contents/${1}" \
	--data-raw '
		{
			"contentFields": [
				{
				"contentFieldValue": {
					"data": "<p>Goo</p>"
				},
				"name": "content"
				}
			],
			"contentStructureId": "'"${2}"'",
			"title": "Baker Article"
		}' \
	--header "Content-Type: application/json" \
	--request "PUT" \
	--user "test@liferay.com:learn"

StructuredContent_PUT_ById.java

コマンド:

java -classpath .:* -DcontentStructureId=1234 -DstructuredContentId=5678 StructuredContent_PUT_ById

コード:

public static void main(String[] args) throws Exception {
	StructuredContentResource.Builder builder =
		StructuredContentResource.builder();

	StructuredContentResource structuredContentResource =
		builder.authentication(
			"test@liferay.com", "learn"
		).build();

	StructuredContent structuredContent =
		structuredContentResource.putStructuredContent(
			Long.valueOf(System.getProperty("structuredContentId")),
			new StructuredContent() {
				{
					contentFields = new ContentField[] {
						new ContentField() {
							{
								contentFieldValue =
									new ContentFieldValue() {
										{
											data = "<p>Goo</p>";
										}
									};
								name = "content";
							}
						}
					};
					contentStructureId = Long.valueOf(
						System.getProperty("contentStructureId"));
					title = "Dog Article";
				}
			});

	System.out.println(structuredContent);
}

Webコンテンツ記事の削除

StructuredContentサービスでDELETEメソッドを使用して、Webコンテンツ記事を削除します。 StructuredContent_DELETE_ById.java|sh] スクリプトサンプルでは、Webコンテンツidを使用してWebコンテンツを削除しています。

Important

REST APIを使用してWebコンテンツを削除すると、Liferay DXPゴミ箱 を使用せずに、永久に削除されます。

StructuredContent_DELETE_ById.sh

コマンド:

./StructuredContent_DELETE_ById.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-delivery/v1.0/structured-contents/${1}" \
	--request "DELETE" \
	--user "test@liferay.com:learn"

StructuredContent_DELETE_ById.java

コマンド

java -classpath .:* -DstructuredContentId=1234 StructuredContent_DELETE_ById

コード:

public static void main(String[] args) throws Exception {
	StructuredContentResource.Builder builder =
		StructuredContentResource.builder();

	StructuredContentResource structuredContentResource =
		builder.authentication(
			"test@liferay.com", "learn"
		).build();

	structuredContentResource.deleteStructuredContent(
		Long.valueOf(System.getProperty("structuredContentId")));
}

その他のWebコンテンツとWebコンテンツフォルダーのサービス

他のcURLコマンドとJavaクラスは、その他の StructuredContentStructuredContentFolder サービスの詳細を示しています。 WebコンテンツAPIの基本 でそれらを確認できます。

ファイル 説明
StructuredContentFolder_GET_ById.[java\|sh] Webコンテンツフォルダーのフィールドを一覧表示します。
StructuredContentFolders_GET_FromSite.[java\|sh] サイト内のすべてのWebコンテンツフォルダーを一覧表示します。
StructuredContentFolder_POST_ToSite.[java\|sh] Webコンテンツフォルダーをサイトに投稿します。
StructuredContentFolder_PATCH_ById.[java\|sh] Webコンテンツフォルダーを更新します。
StructuredContentFolder_PUT_ById.[java\|sh] Webコンテンツフォルダーを置き換えます。
StructuredContentFolder_DELETE_ById.[java\|sh] Webコンテンツフォルダーを削除します。
StructuredContent_POST_ToStructuredContentFolder.[java\|sh] Webコンテンツ記事をフォルダーに投稿します。
Important

REST APIを使用してWebコンテンツフォルダーを削除すると、フォルダとそのコンテンツは Liferay DXPゴミ箱 を使わずに永久に削除されます。

関連情報