実際の状況では、操作に対するフィードバックをユーザーに返す必要があるでしょう。そのためには、ビジネスコンポーネントを使用して、データベーステーブル内で操作を行い、その操作の結果を呼び出し元のユーザーに返すことができます。
この例では、AddCustomer プロシージャーを GeneXus で REST Web サービスとして宣言しています。
- Expose as web service = True
- Rest protocol = True
このプロシージャーは、Customer テーブルに追加する顧客の情報を含む SDT をパラメーターとして受け取ります。
また、変数 &messages でエラーや警告のメッセージを返し、操作に関するフィードバックをユーザーに返します。
重要な注記: REST プロシージャーは、単純、SDT、コレクションなど、あらゆるタイプのパラメーターを受け取ることができます。
Parm ルール:
parm(in:&Customersdt,out:&messages);
ソース:
&Customer.Load(&Customersdt.CustomerId)
if &Customer.Fail()
&Customer = new()
&Customer.CustomerId = &Customersdt.CustomerId
endif
&Customer.CustomerName = &Customersdt.CustomerName
&Customer.CustomerBirthDate = &Customersdt.CustomerBirthDate
&Customer.CustomerPayDate = &Customersdt.CustomerPayDate
&customer.CustomerPhoto = &customersdt.CustomerPhoto
&customer.Save()
&messages = &customer.GetMessages()
commit
変数 &Customersdt は、構造化データタイプ Customersdt に基づいています。
この REST プロシージャーの GeneXus クライアントは次のようになります。
&httpclient.Host = &host
&httpclient.Port = &port
&httpclient.BaseUrl = &baseurl
&body = '{"Customersdt":' + &customersdt.ToJson() + '}'
&httpclient.AddHeader('Content-type','application/json')
&httpclient.AddString(&body)
&httpclient.Execute('POST','AddCustomer')
その後、HTTP クライアントの応答を処理します。
注:
1. REST プロシージャー呼び出しの BaseURL は <Web アプリケーション名>/rest です。
2. HTTP リクエストは JSON 形式になります。プロシージャーは SDT (構造化データタイプ) を受け取るため、想定される JSON の前に SDT の名前が付きます。これは、大文字と小文字がプロシージャーのパラメーターと同じになっている必要があります (&Customersdt)。
HTTP リクエストは次のようになります。
{"Customersdt":{"CustomerId":129,"CustomerName":"Rodolfo","CustomerBirthDate":"1976-08-05","CustomerPayDate":"1976-08-05T00:31:00","CustomerPhoto":"12356"}}
GeneXus の REST Web サービス
|