REST API를 사용하여 규칙 세트 호출
클라이언트 애플리케이션은 REST API를 통해 의사결정 서비스 규칙 세트를 호출합니다. 표준 HTTP
GET, POST, PUT, DELETE 명령을 사용합니다. 시작하기 전에
클라이언트 애플리케이션은 클라우드 포털에 대한 인증 신임 정보를 전달해야 합니다 ( 클라이언트 애플리케이션에 대한 서비스 신임 정보참조).
이 태스크에 대한 정보
서버 내 규칙 세트에 대한 엔드포인트 URI는 다음과 같습니다.
https://<vhostname>.bpm.ibmcloud.com/odm/<odm_on_cloud_environment>/DecisionService/rest/v1/<ruleset_path>다음 목록은 URI의 부분을 설명합니다.
- <vhostname>: 의사결정 서비스에 대한 호스트의 이름 역할을 합니다.
- <odm_on_cloud_environment>: 의사결정 서비스 규칙 세트의 위치(개발, 테스트 또는 프로덕션 환경)에 따라 dev, test 또는 prod을(를) 사용합니다.
- <ruleset_path>: 의사결정 서비스 규칙 실행의 경로를 제공하며 다음 형식 중 하나여야 합니다.
- ruleAppName/rulesetName
- ruleAppName/ruleAppVersion/rulesetName
- ruleAppName/ruleAppVersion/rulesetName/rulesetVersion
다음 예는 MiniloanServiceRuleset에 대한 URI를 보여 줍니다.
https://vhost005.bpm.ibmcloud.com/odm/dev/DecisionService/rest/v1/
MiniloanService/MiniloanServiceRuleset규칙 집합의 실행은 지정된 엔드포인트 URI의 HTTP POST를 통해 실행됩니다. 인증을 위한 사용자 이름 및 비밀번호는 권한 부여 헤더와 함께 전달됩니다(REST 및 SOAP 호출을 위한 인증 참조).예
MiniloanServiceRuleset 를 호출하는 Java™ 코드를 보여줍니다. 이 예에서는 Apache HTTPClient v4.3을 사용합니다.import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
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 <vhostname> with the name of the host of the cloud portal
String endpointURI = "https://<vhostname>.bpm.ibmcloud.com/odm/dev/DecisionService/rest/v1/MiniloanService/MiniloanServiceRuleset";
// Replace "loginID" with the functional ID of the service account you are using to authenticate with your tenant in the Cloud
String userName = "loginID";
// Replace "password" with the password of the service account you are using to authenticate with your tenant in the Cloud
String password = "password";
String credentials = userName + ":" + password;
String encodedValue = Base64.encodeBase64String(credentials.getBytes());
String authorization = "Basic " + new String(encodedValue);
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);
// Add the basic authentication header
httpPost.addHeader("Authorization", authorization);
httpPost.setEntity(new StringEntity(payload));
httpPost.addHeader("Content-Type", contentType);
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();
}
}
}