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

国別APIの基礎

Liferay 7.4 U24+ および GA24+

Liferay の Rest API を使用して国を作成および管理します。

国を追加する

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

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

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

次に、以下の手順に従います。

  1. Countries API Basicsをダウンロードして解凍します。

    curl https://resources.learn.liferay.com/examples/liferay-g6m8.zip -O
    
    unzip liferay-g6m8.zip
    
  2. cURL スクリプトを使用して、インスタンスに新しい国を追加します。 コマンドラインで、curlフォルダに移動します。 Countries_POST_ToInstance.sh スクリプトを実行します。

    ./Countries_POST_ToInstance.sh
    

    JSON 応答には、新しい国が追加されたことが示されています。

    {
      "a2" : "AB",
      "a3" : "ABL",
      "active" : true,
      "billingAllowed" : true,
      "groupFilterEnabled" : false,
      "id" : 43501,
      "name" : "Foo",
      "number" : 1234,
      "position" : 0.0,
      "regions" : [ ],
      "shippingAllowed" : true,
      "subjectToVAT" : false,
      "title_i18n" : { },
      "zipRequired" : true
    }
    
  3. RESTサービスは、Javaクライアントを使って呼び出すこともできます。 curl フォルダから、 java フォルダに移動します。 ソースファイルを次のようにコンパイルします。

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

    java -classpath .:* Countries_POST_ToInstance
    

cURLコマンドの検証

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

curl \
	"http://localhost:8080/o/headless-admin-address/v1.0/countries" \
	--data-raw '
		{
			"a2": "AB",
			"a3": "ABL",
			"name": "Foo",
			"number": "1234"
		}' \
	--header "Content-Type: application/json" \
	--request "POST" \
	--user "test@liferay.com:learn"

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

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

ここでは、デモのために基本的な認証を使用しています。 本番環境では、 OAuth2経由でユーザーを認証する必要があります。 OAuth2 を使用するサンプル React アプリケーションについては、 OAuth2 を使用してユーザーを承認する を参照してください。

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

Javaクラスを調べる

Countries_POST_ToInstance.java クラスは、Country 関連のサービスを呼び出して国を追加します。

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

	CountryResource countryResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Country country = countryResource.postCountry(
		new Country() {
			{
				a2 = "AB";
				a3 = "ABL";
				name = "Foo";
				number = 1234;
			}
		});

	System.out.println(country);
}

このクラスは、わずか3行のコードでRESTサービスを呼び出します。

行(省略形)説明
CountryResource.Builder builder = ...Builder を取得して、 CountryResource サービス インスタンスを生成します。
CountryResource countryResource = builder.authentication(...).build();基本認証を指定し、 CountryResource サービス インスタンスを生成します。
Country country = countryResource.postCountry(...);countryResource.postCountry メソッドを呼び出し、データを post に渡します。

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

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

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

重要

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

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

インスタンスから国を取得する

次の cURL または Java コマンドを実行すると、国を一覧表示できます。

Countries_GET_FromInstance.sh

コマンド:

./Countries_GET_FromInstance.sh

コード:

curl \
	"http://localhost:8080/o/headless-admin-address/v1.0/countries" \
	--user "test@liferay.com:learn"

Countries_GET_FromInstance.java

コマンド:

java -classpath .:* Countries_GET_FromInstance

コード:

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

	CountryResource countryResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Page<Country> page = countryResource.getCountriesPage(
		null, null, Pagination.of(1, 2), null);

	System.out.println(page);
}

インスタンスの Country オブジェクトが JSON に表示されます。

国を取得する

次の cURL または Java コマンドを使用して特定の国を取得します。

ヒント

Countries_GET_FromInstance.[java|sh] を使用して、インスタンス Country ID を取得します。

Countries_GET_ById.sh

コマンド:

./Countries_GET_ById.sh 1234

コード:

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

Countries_GET_ById.java

コマンド:

java -classpath .:* -DcountryId=1234 Countries_GET_ById

コード:

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

	CountryResource countryResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	System.out.println(
		countryResource.getCountry(
			Long.valueOf(System.getProperty("countryId"))));
}

Country フィールドが JSON に表示されます。

国をパッチする

次の cURL および Java コマンドを使用して、既存の国を部分的に編集します。 1234 をあなたの国のIDに置き換えてください。

Countries_PATCH_ById.sh

コマンド:

./Countries_PATCH_ById.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}" \
	--data-raw '
		{
			"name": "Bar"
		}' \
	--header "Content-Type: application/json" \
	--request "PATCH" \
	--user "test@liferay.com:learn"

Countries_PATCH_ById.java

コマンド:

java -classpath .:* -DcountryId=1234 Countries_PATCH_ById

コード:

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

	CountryResource countryResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Country country = countryResource.patchCountry(
		Long.valueOf(System.getProperty("countryId")),
		new Country() {
			{
				name = "Bar";
			}
		});

	System.out.println(country);
}

国を置く

次の cURL および Java コマンドを使用して、既存の国を完全に上書きします。 1234 をあなたの国のIDに置き換えてください。

Countries_PUT_ById.sh

コマンド:

./Countries_PUT_ById.sh 1234

コード:

curl \
	"http://localhost:8080/o/headless-admin-address/v1.0/countries/${1}" \
	--data-raw '
		{
			"a2": "AB",
			"a3": "ABL",
			"name": "Goo",
			"number": "1234"
		}' \
	--header "Content-Type: application/json" \
	--request "PUT" \
	--user "test@liferay.com:learn"

Countries_PUT_ById.java

コマンド:

java -classpath .:* -DcountryId=1234 Countries_PUT_ById

コード:

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

	CountryResource countryResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	Country country = countryResource.putCountry(
		Long.valueOf(System.getProperty("countryId")),
		new Country() {
			{
				a2 = "AB";
				a3 = "ABL";
				name = "Goo";
				number = 1234;
			}
		});

	System.out.println(country);
}

国を削除する

次の cURL および Java コマンドを使用して、既存の国を削除します。 1234 をあなたの国のIDに置き換えてください。

Countries_DELETE_ById.sh

コマンド:

./Countries_DELETE_ById.sh 1234

コード:

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

Countries_DELETE_ById.java

コマンド

java -classpath .:* -DcountryId=1234 Countries_DELETE_ById

コード:

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

	CountryResource countryResource = builder.authentication(
		"test@liferay.com", "learn"
	).build();

	countryResource.deleteCountry(
		Long.valueOf(System.getProperty("countryId")));
}

API エクスプローラー には、すべての Country サービスとスキーマが表示され、各サービスを試すためのインターフェースがあります。