REST API を使用した意思決定サービスの呼び出し
クライアントは、Operational Decision Manager の REST API を介して意思決定サービスを呼び出します。クライアントは、Web ブラウザーがリモート・サーバーと対話するときに使用する標準の HTTP のGET、POST、PUT、および DELETE の各コマンドを使用して、意思決定サービスを呼び出します。
このタスクについて
Operational Decision
Manager サーバーにおける意思決定サービスのエンドポイント URI では、以下の形式が使用されます。
http://<host>:<port>/DecisionService/rest/v1/<ruleset_path>
以下のリストでは、URI の部分を説明します。
- <host>: 意思決定サービスのホストの名前となります。
- <port>: ポート番号
- <ruleset_path>: 意思決定サービスのルールを実行するためのパスを提供し、以下のいずれかの形式を取る必要があります。
- ruleAppName/rulesetName
- ruleAppName/ruleAppVersion/rulesetName
- ruleAppName/ruleAppVersion/rulesetName/rulesetVersion
以下の例は、MiniloanServiceRuleset の URI を示しています。
http://localhost:9080/DecisionService/rest/v1/
MiniloanService/MiniloanServiceRuleset
意思決定サービスのルール・セットの実行は、指定されたエンドポイント URI で HTTP POST を介して実行されます。例
以下の例は、Operational Decision Manager サーバー上の MiniloanService 意思決定サービスを呼び出す Java™ コードを示しています。この例では、Apache HTTPClient v4.3 を使用しています。
import java.io.IOException;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class DecisionServiceExecution {
public static void main(String[] args) throws ClientProtocolException, IOException {
// Replace <host> and <port>
String endpointURI = "http://<host>:<port>/DecisionService/rest/v1/MiniloanService/MiniloanServiceRuleset";
String contentType = "application/json";
// Set the borrower and the loan
String payload = "{¥"borrower¥": {" +
"¥"name¥": ¥"John¥"," +
"¥"creditScore¥": 600," +
"¥"yearlyIncome¥": 80000" +
"}," +
"¥"loan¥": {" +
"¥"amount¥": 500000," +
"¥"duration¥": 240," +
"¥"yearlyInterestRate¥": 0.05" +
"}" +
"}";
CloseableHttpClient client = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(endpointURI);
httpPost.setEntity(new StringEntity(payload));
CloseableHttpResponse response = client.execute(httpPost);
try {
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
System.err.println("Status Code: " + response.getStatusLine().getStatusCode());
System.err.println("Status Line: " + response.getStatusLine());
String responseEntity = EntityUtils.toString(response.getEntity());
System.err.println("Response Entity: " + responseEntity);
throw new RuntimeException(
"An error occurred when invoking Decision Service at: "
+ endpointURI
+ "¥n"
+ response.getStatusLine() + ": " + responseEntity);
} else {
String result = EntityUtils.toString(response.getEntity());
System.out.println("Result: " + result);
}
} finally {
if (response != null) {
response.close();
}
}
} finally {
client.close();
}
}
}