Invocación de un servicio de decisiones utilizando la API REST
El cliente llama al servicio de decisiones mediante la API REST de Operational Decision Manager. El cliente llama al servicio de decisiones utilizando los mandatos HTTP estándar GET, POST, PUT y DELETE, que los navegadores web utilizan cuando interactúan con servidores remotos.
Acerca de esta tarea
EL URI de punto final para un servicio de decisiones en el servidor
Operational Decision
Manager utiliza el formato siguiente:
http://<host>:<puerto>/DecisionService/rest/v1/<vía_acceso_conjunto_reglas>
La lista siguiente explica las partes del URI:
- <host>: nombre del host para el servicio de decisiones.
- <puerto>: número de puerto
- <vía_acceso_conjunto_reglas>: vía de acceso para la ejecución de reglas del servicio de decisiones, con uno de los
siguientes formatos:
- ruleAppName/rulesetName
- ruleAppName/ruleAppVersion/rulesetName
- ruleAppName/ruleAppVersion/rulesetName/rulesetVersion
El ejemplo siguiente muestra un URI para
MiniloanServiceRuleset:
http://localhost:9080/DecisionService/rest/v1/
MiniloanService/MiniloanServiceRuleset
La ejecución del conjunto de reglas del servicio de decisiones se lleva a cabo mediante un HTTP POST en el URI de punto final
especificado.Ejemplo
El ejemplo siguiente muestra código Java™ que invoca el servicio de decisiones MiniloanService en el servidor Operational Decision Manager. El ejemplo utiliza 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 {
// Sustituya <host> y <puerto>
String endpointURI = "http://<host>:<port>/DecisionService/rest/v1/MiniloanService/MiniloanServiceRuleset";
String contentType = "application/json";
// Establecer el prestatario y el préstamo
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("Código de estado: " + response.getStatusLine().getStatusCode());
System.err.println("Línea de estado: " + response.getStatusLine());
String responseEntity = EntityUtils.toString(response.getEntity());
System.err.println("Entidad de respuesta: " + responseEntity);
throw new RuntimeException(
"Se ha producido un error al invocar el servicio de decisiones en: "
+ 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();
}
}
}