次の例は、ID に基づいて Customer に関する情報を返す REST GeneXus プロシージャーを示します。
次のプロパティを設定すると、GeneXus プロシージャー (「GetCustomer」) が REST として宣言されます:
- Expose as Web Service = TRUE
- Rest Protocol = True
次に示すように、コードは非常に単純です:
Parm ルール:
parm(in:&CustomerId,out:&CustomerName,out:&CustomerBirthDate,out:&CustomerPayDate);
ソースコード:
for each
where CustomerId = &CustomerId
&customerName = CustomerName
&CustomerBirthDate = CustomerBirthDate
&CustomerPayDate = CustomerPayDate
endfor
GeneXus クライアントは、たとえば次のようになります:
&httpclient.Host =&host // &httpclient is an HTTPClient variable data type
&httpclient.Port = &port
&httpclient.BaseUrl = &baseurl //Example: /webappname/rest/
&customersdt.CustomerId= &customerId
&body = &customersdt.ToJson()
&httpclient.AddHeader('Content-type','application/json')
&httpclient.AddString(&body)
&httpclient.Execute('POST','GetCustomer')
&lvc = &httpclient.ToString()
応答は、次のように JSON 形式になります:
{"CustomerBirthDate":"1989-06-03","CustomerName":"Francisco","CustomerPayDate":"1995-03-21T18:54:00"}
このため、結果は対応する構造を持つ SDT (Structured Data Type オブジェクト) に JSON 応答をロードして処理する必要があります。
- JSON コード (この例では変数 &body) には、parm ルールでパラメーターと宣言されたアイテムのみを含めることができます。
- REST プロシージャーの parm ルールで定義されているとおり、JSON コード内の値はキャメルケースにする必要があります。
一般的には次のように設定します:
|
サンプル |
URL |
http://localhost:8080/applicationname/rest/GetCustomer |
本文のパラメーター |
Content-Type: application/json; charset=UTF-8
{"CustomerId":1} |
メソッド |
Post |
GetCustomer オブジェクトを呼び出している 1 つ前のサンプルに基づいたサンプル要求は次のとおりです:
POST /Demo20140425U4JavaScriptEnvironment1/rest/GetCustomer HTTP/1.1
Content-Length: 17
Content-Type: application/json; charset=UTF-8
Host: localhost:8181
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.3.1 (java 1.5)
{"CustomerId":1}
応答は次のようになります:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=D737067FECD79CD466F29877D3B74375; Path=/Demo20140425U4JavaScriptEnvironment1/; HttpOnly
Last-Modified: Wed, 23 Jul 2014 06:29:04 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 23 Jul 2014 06:29:05 GMT
58
{"CustomerName":"miura","CustomerBirthDate":"2014-07-01","CustomerPayDate":"2014-07-01"}
0
GeneXus の REST Web サービス
|