oo

各国APIの基本

Liferay 7.4 U24+ および GA24+

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

国の追加

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

docker run -it -m 8g -p 8080:8080 liferay/dxp:2024.q1.1。

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

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

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

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

    ./Country_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. Country_POST_ToInstance.javaクラスを実行する:

    java -classpath .:* Country_POST_ToInstance
    

cURLコマンドの検証

Country_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" 基本的な認証情報
note

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

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

Javaクラスを調べる

Country_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 = ... CountryResourceサービスインスタンスを生成するためのBuilder を取得する。
CountryResource countryResource = builder.authentication(...).build(); 基本認証を指定し、CountryResource サービスインスタンスを生成する。
Country country = countryResource.postCountry(...); countryResource.postCountryメソッドを呼び出し、データをpostに渡す。

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

note

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

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

important

サービスの詳細は 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);
}

インスタンスの Country オブジェクトは JSON で表示される。

国を手に入れる

以下のcURLまたはJavaコマンドで特定の国を取得する。

tip

インスタンスの ID を取得するには Countries_GET_FromInstance.[java|sh] を使用してください。

Country_GET_ById.sh

コマンド:

./Country_GET_ById.sh 1234

コード:

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

Country_GET_ById.java

コマンド:

java -classpath .:* -DcountryId=1234 Country_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に置き換えてください。

Country_PATCH_ById.sh

コマンド:

./Country_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"

Country_PATCH_ById.java

コマンド:

java -classpath .:* -DcountryId=1234 Country_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に置き換えてください。

Country_PUT_ById.sh

コマンド:

./Country_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"

Country_PUT_ById.java

コマンド:

java -classpath .:* -DcountryId=1234 Country_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に置き換えてください。

Country_DELETE_ById.sh

コマンド:

./Country_DELETE_ById.sh 1234

コード:

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

Country_DELETE_ById.java

コマンド

java -classpath .:* -DcountryId=1234 Country_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 Explorerには、すべての Country サービスとスキーマが表示され、各サービスを試すためのインターフェイスが用意されている。