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

http://localhost:8080 に、メールアドレス test@liferay.com とパスワード test を使用して 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 クラスは、 CountryResource サービスを呼び出すことで国を追加します。

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 = ...ビルダー を取得して、 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エクスプローラーで確認できます。

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

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

重要

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

以下は、cURLとJavaを使用して他の の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);
}

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

国を取得する

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

ヒント

Countries_GET_FromInstance.[java|sh] を使用して、インスタンス の国 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"))));
}

フィールドは 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エクスプローラー は、 のすべてのサービスとスキーマを表示し、各サービスを試すためのインターフェースを備えています。