?1:指定请求endpoint为/$batch
2:构建请求内容:
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*
import java.util.UUID;
import static groovy.json.JsonOutput.*
// @Grab('com.google.code.gson:gson:2.8+')
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
class BatchRequest {
def requests
}
class Request{
String id
String method
String url
def headers
def body
}
class IFInstanceStruc {
String EndDateTime
int TotalNum
int SuccessNum
int ErrorNum
int Status
}
class HttpHeader {
@SerializedName("content-type")
String contenttype
}
def HttpHeader getCommonHttpHeader(){
return new HttpHeader(
contenttype : "application/json"
);
}
def Message processData(Message message) {
//Body
def requests = [];
def body = message.getBody(String);
def logBody = new JsonSlurper().parseText(body);
BatchRequest batchRequest = new BatchRequest(
requests:[]
);
// IFInstance PATCH
IFInstanceStruc ifInstanceBody = new IFInstanceStruc(
EndDateTime : logBody.EndDateTime,
TotalNum : logBody.TotalNum,
SuccessNum : logBody.SuccessNum,
ErrorNum : logBody.ErrorNum,
Status : logBody.Status
);
// def ifInstanceBodyJSON = new Gson().toJson(ifInstanceBody)
Request ifInstanceRequest = new Request(
id : UUID.randomUUID().toString(),
method: "PATCH",
url : "/IFInstance(" + logBody.ID + ")",
headers : getCommonHttpHeader(),
body : ifInstanceBody
)
requests.add(ifInstanceRequest);
// IFItem PATCH
logBody.toItems.each {
Request ifItemRequest = new Request(
id: UUID.randomUUID().toString(),
method: "POST",
url: "/IFInstance(" + logBody.ID + ")/toItems",
headers: getCommonHttpHeader(),
body: it
)
requests.add(ifItemRequest)
}
// Add to Batch Request
batchRequest.requests = requests;
def gson = new Gson().toJson(batchRequest)
message.setBody(gson);
//设置 http header
message.setHeader("Content-Type", "application/json")
message.setHeader("Accept", "*/*")
return message;
}
?生成请求报文格式如下:
{
"requests": [
{
"id": "c9863906-5889-433d-96df-93551b56bcb3",
"method": "PATCH",
"url": "/IFInstance(4c38271a-17ba-462e-803e-0475b37a5145)",
"headers": {
"content-type": "application/json"
},
"body": {
"EndDateTime": "2021-12-01T06:01:25.621Z",
"TotalNum": 0,
"SuccessNum": 0,
"ErrorNum": 0,
"Status": 1
}
},
{
"id": "101c7ebf-a5b2-47ca-8fdb-62fb2db0e98f",
"method": "POST",
"url": "/IFInstance(4c38271a-17ba-462e-803e-0475b37a5145)/toItems",
"headers": {
"content-type": "application/json"
},
"body": {
"Result": "S",
"Message": "成功処理 購買情報マスタ登録されました",
"Source": "S4",
"Target": "MMSS",
"TransMethod": "Webservice",
"toMessages": [
{
"HGUID": "4c38271a-17ba-462e-803e-0475b37a5145",
"MsgClass": "MMSS_A006",
"MsgType": "S",
"MsgNo": "11",
"Message": "成功処理 購買情報マスタ登録されました"
}
]
}
}
]
}
批处理请求的使用方式可以参考以下链接:
Combine multiple requests in one HTTP call using JSON batching - Microsoft Graph | Microsoft Docshttps://docs.microsoft.com/en-us/graph/json-batching
|