289.11. Cryostat Composite API를 사용하여 여러 요청을 일괄로 제출
Composite API 일괄 처리 작업(복합 API 배치작업)을 사용하면 여러 요청을 일괄 처리로 누적한 다음 한 번에 제출하여 여러 개별 요청의 라운드트립 비용을 절감할 수 있습니다. 그런 다음 각 응답은 순서가 보존된 응답 목록으로 수신되므로 9번째 요청 응답이 응답 위치에 있습니다.
결과는 API마다 다를 수 있으므로 요청 결과는 java.lang.Object 로 제공됩니다. 대부분의 경우 결과는 문자열 키와 값이 있는 java.util.Map 또는 기타 java.util.Map 값이 value입니다. JSON 형식으로 만든 요청에는 일부 유형 정보가 있습니다(즉, 어떤 값이 문자열이고 숫자인지 알 수 있음) 일반적으로 해당 정보에 더 적합합니다. 응답은 XML과 JSON마다 다를 수 있습니다. 이는 Cryostat API의 응답 때문입니다. 따라서 응답 처리 코드를 변경하지 않고 형식 간에 전환할 때는 주의해야 합니다.
예를 들면 다음과 같습니다.
final String acountId = ...
final SObjectBatch batch = new SObjectBatch("38.0");
final Account updates = new Account();
updates.setName("NewName");
batch.addUpdate("Account", accountId, updates);
final Account newAccount = new Account();
newAccount.setName("Account created from Composite batch API");
batch.addCreate(newAccount);
batch.addGet("Account", accountId, "Name", "BillingPostalCode");
batch.addDelete("Account", accountId);
final SObjectBatchResponse response = template.requestBody("salesforce:composite-batch?format=JSON", batch, SObjectBatchResponse.class);
boolean hasErrors = response.hasErrors(); // if any of the requests has resulted in either 4xx or 5xx HTTP status
final List<SObjectBatchResult> results = response.getResults(); // results of three operations sent in batch
final SObjectBatchResult updateResult = results.get(0); // update result
final int updateStatus = updateResult.getStatusCode(); // probably 204
final Object updateResultData = updateResult.getResult(); // probably null
final SObjectBatchResult createResult = results.get(1); // create result
@SuppressWarnings("unchecked")
final Map<String, Object> createData = (Map<String, Object>) createResult.getResult();
final String newAccountId = createData.get("id"); // id of the new account, this is for JSON, for XML it would be createData.get("Result").get("id")
final SObjectBatchResult retrieveResult = results.get(2); // retrieve result
@SuppressWarnings("unchecked")
final Map<String, Object> retrieveData = (Map<String, Object>) retrieveResult.getResult();
final String accountName = retrieveData.get("Name"); // Name of the retrieved account, this is for JSON, for XML it would be createData.get("Account").get("Name")
final String accountBillingPostalCode = retrieveData.get("BillingPostalCode"); // Name of the retrieved account, this is for JSON, for XML it would be createData.get("Account").get("BillingPostalCode")
final SObjectBatchResult deleteResult = results.get(3); // delete result
final int updateStatus = deleteResult.getStatusCode(); // probably 204
final Object updateResultData = deleteResult.getResult(); // probably null