SOAP服务多查询参数处理:多CustomerID请求构造的正确方式
Apigee代理调用SOAP服务的请求格式问题
我有一个Apigee代理,用于调用SOAP服务,通过客户ID查询数据库并返回关联的客户详情。现在需要确定构造SOAP请求的正确方式,CustomerIDs的值为1,2,3,4,以下是两种候选格式:
格式一:单节点传递逗号分隔值
<soap:Body> <v1:getCustomerInfobyIDRequest> <CustomerID>{CustomerIDs}</CustomerID> </v1:getCustomerInfobyIDRequest> </soap:Body>
格式二:多节点传递单个ID
<soap:Body> <v1:getCustomerInfobyIDRequest> <CustomerID>1</CustomerID> <CustomerID>2</CustomerID> <CustomerID>3</CustomerID> <CustomerID>4</CustomerID> </v1:getCustomerInfobyIDRequest> </soap:Body>
结论:取决于目标SOAP服务的WSDL定义
- 如果服务的WSDL中明确
CustomerID元素的maxOccurs属性大于1(支持重复节点),那么格式二是标准且正确的SOAP请求方式,这是SOAP协议传递多值参数的常规做法。 - 只有当服务端自定义实现了接受逗号分隔字符串作为
CustomerID的输入时,格式一才适用,但这种属于非标准场景,很少见。
在Apigee中生成正确请求的方法
如果需要动态生成多个CustomerID节点,可以使用Apigee的JavaScript政策实现,示例代码如下:
// 从变量获取CustomerIDs并拆分 var customerIDs = context.getVariable("CustomerIDs").split(","); // 构建SOAP请求体 var soapBody = '<soap:Body><v1:getCustomerInfobyIDRequest>'; customerIDs.forEach(function(id) { soapBody += '<CustomerID>' + id.trim() + '</CustomerID>'; }); soapBody += '</v1:getCustomerInfobyIDRequest></soap:Body>'; // 将生成的请求体设置到request.content变量 context.setVariable("request.content", soapBody);
内容的提问来源于stack exchange,提问作者Brylle
相关产品推荐
相关产品推荐

