Regions API の基本

Regions API の基本

Liferay 7.4 U24以降とGA24以降で利用可能

Liferay の REST API を使用して、地域を作成および管理します。

リージョンの追加

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

docker run -it -m 8g -p 8080:8080 liferay/dxp:7.4.13-u55。

メールアドレス[email protected]_とパスワード_test_を使用して、http://localhost:8080でLiferayにサインインしてください。 プロンプトが表示されたら、パスワードを _learn_に変更します。

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

  1. Regions API Basics をダウンロードし、解凍してください。

    curl https://resources.learn.liferay.com/dxp/latest/ja/users-and-permissions/developer-guide/liferay-r2p3.zip -O
    
    unzip liferay-r2p3.zip
    
  2. cURL スクリプトを使用して、国に新しい地域を追加します。 コマンドラインで、 curl フォルダに移動します。 Region_POST_ToCountry.sh スクリプトを実行します。

    ./Region_POST_ToCountry.sh 1234
    

    1234 を国のIDに置き換えてください。 Get Countries from Instance を使用して、ID のリストを取得します。

    JSONレスポンスは、新しい地域が追加されたことを示します。

    "active" : true,
    "countryId" : 43501,
    "id" : 43503,
    "name" : "Foo",
    "position" : 0.0,
    "regionCode" : "ABL",
    "title_i18n" : { }
    
    
  3. RESTサービスは、Javaクライアントを使って呼び出すこともできます。 curl フォルダから、 java フォルダに移動します。 ソースファイルをコンパイルします。

    javac -classpath .:* *.java
    
  4. Region_POST_ToCountry.java クラスを実行します。

    java -classpath .:* -DcountryId=1234 Region_POST_ToCountry
    

cURLコマンドの検証

Region_POST_ToCountry.sh スクリプトは、cURL コマンドで REST サービスを呼び出します。

curl \
	-H "Content-Type: application/json" \
	-X POST \
	"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}/regions" \
	-d "{\"name\": \"Foo\", \"regionCode\": \"ABL\"}" \
	-u "[email protected]:learn"

ここでは、コマンドの引数を紹介します。

引数 説明
-H "Content-Type: application/json" リクエストボディのフォーマットがJSONであることを示します。
-X POST 指定されたエンドポイントで起動するHTTPメソッド
"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}/regions" RESTサービスのエンドポイント
-d "{\"name\": \"Foo\", \"regionCode\": \"ABL\"}" お客様が掲載を希望するデータ
-u "[email protected]:learn" 基本的な認証情報
note

ここでは、デモのためにベーシック認証を使用しています。 本番環境では、 OAuth2 を使ってユーザーを認証する必要があります。 OAuth2を利用したReactアプリケーションのサンプルは、OAuth2を使ってユーザーを認証するをご参照ください。

他のcURLコマンドも同様のJSON引数を使用しています。

Javaクラスを調べる

Region_POST_ToCountry.java クラスは、Region 関連サービスを呼び出すことで地域を追加します。

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

	RegionResource regionResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	Region region = regionResource.postCountryRegion(
		Long.valueOf(System.getProperty("countryId")),
		new Region() {
			{
				name = "Foo";
				regionCode = "ABL";
			}
		});

	System.out.println(region);
}

このクラスは、次の3行のコードのみを使用してRESTサービスを呼び出します。

行(省略形) 説明
RegionResource.Builder builder = ... RegionResource サービスインスタンスを生成するための Builder を取得する。
RegionResource regionResource = builder.authentication(...).build(); 基本認証を指定し、 RegionResource サービスインスタンスを生成します。
Region region = regionResource.postRegion(...); regionResource.postRegion メソッドを呼び出し、データをpostに渡します。

プロジェクトには、依存関係としてcom.liferay.headless.admin.address.client.jarファイルが含まれていることに注意してください。 すべてのRESTアプリケーションのクライアントJAR依存関係情報は、/o/apiでインストール先のAPIエクスプローラーで確認できます。

note

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

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

important

サービスの詳細は RegionResource を参照ください。

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

インスタンスからリージョンを取得する

以下のcURLまたはJavaコマンドを実行することで、リージョンの一覧を表示することができます。

Regions_GET_FromInstance.sh

コマンド:

./Regions_GET_FromInstance.sh

コード:

curl \
	"http://localhost:8080/o/headless-admin-address/v1.0/regions" \
	-u "[email protected]:learn"

Regions_GET_FromInstance.java

コマンド:

java -classpath .:* Regions_GET_FromInstance

コード:

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

	RegionResource regionResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	Page<Region> page = regionResource.getRegionsPage(
		null, null, Pagination.of(1, 2), null);

	System.out.println(page);
}

Instance の Region オブジェクトが JSON で表示されます。

リージョンを取得する

以下のcURLまたはJavaコマンドで、特定の地域を取得します。

tip

インスタンスの Region ID を取得するには、 Regions_GET_FromInstance.[java|sh] を使用します。

Region_GET_ById.sh

コマンド:

./Region_GET_ById.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-admin-address/v1.0/regions/${1}" \
	-u "[email protected]:learn"

Region_GET_ById.java

コマンド:

java -classpath .:* -DregionId=1234 Region_GET_ById

コード:

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

	RegionResource regionResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	System.out.println(
		regionResource.getRegion(
			Long.valueOf(System.getProperty("regionId"))));
}

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

リージョンのパッチ

以下のcURLとJavaコマンドで、既存のRegionの部分編集を行う。 1234 を自分のリージョンIDに置き換えてください。

Region_PATCH_ById.sh

コマンド:

./Region_PATCH_ById.sh 1234

コード:

curl \
	-H "Content-Type: application/json" \
	-X PATCH \
	"http://localhost:8080/o/headless-admin-address/v1.0/regions/${1}" \
	-d "{\"name\": \"Bar\"}" \
	-u "[email protected]:learn"

Region_PATCH_ById.java

コマンド:

java -classpath .:* -DregionId=1234 Region_PATCH_ById

コード:

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

	RegionResource regionResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	Region region = regionResource.patchRegion(
		Long.valueOf(System.getProperty("regionId")),
		new Region() {
			{
				name = "Bar";
			}
		});

	System.out.println(region);
}

リージョンを置く

以下のcURLとJavaコマンドで、既存のリージョンを完全に上書きする。 1234 を自分の地域のIDに置き換えてください。

Region_PUT_ById.sh

コマンド:

./Region_PUT_ById.sh 1234

コード:

curl \
	-H "Content-Type: application/json" \
	-X PUT \
	"http://localhost:8080/o/headless-admin-address/v1.0/regions/${1}" \
	-d "{\"name\": \"Goo\", \"regionCode\": \"ABL\"}" \
	-u "[email protected]:learn"

Region_PUT_ById.java

コマンド:

java -classpath .:* -DregionId=1234 Region_PUT_ById

コード:

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

	RegionResource regionResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	Region region = regionResource.putRegion(
		Long.valueOf(System.getProperty("regionId")),
		new Region() {
			{
				name = "Goo";
				regionCode = "ABL";
			}
		});

	System.out.println(region);
}

リージョンの削除

以下のcURLとJavaのコマンドで、既存のリージョンを削除します。 1234 を自分の地域のIDに置き換えてください。

Region_DELETE_ById.sh

コマンド:

./Region_DELETE_ById.sh 1234

コード:

curl \
	-X DELETE \
	"http://localhost:8080/o/headless-admin-address/v1.0/regions/${1}" \
	-u "[email protected]:learn"

Region_DELETE_ById.java

コマンド

java -classpath .:* -DregionId=1234 Region_DELETE_ById

コード:

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

	RegionResource regionResource = builder.authentication(
		"[email protected]", "learn"
	).build();

	regionResource.deleteRegion(
		Long.valueOf(System.getProperty("regionId")));
}

API Explorer には Region のすべてのサービスとスキーマが表示され、各サービスを試用するためのインターフェイスが用意されています。