Odata 查询批量请求 - 过滤器不工作

Odata Query Batch Request - Filter not working

我们正在使用 SAP SDK 3.25.0 并调用批处理请求读取查询传递一些过滤器。我得到了所有记录的响应,可以看出过滤器没有正常工作。

我已经引用了这个博客 here 但我遇到了同样的解码问题 URL 问题 YY1_QuantityContractTracki?$filter=((CustomerName eq %27Ford%27) and (SalesSchedulingAgreement eq %270030000141%27)) and (PurchaseOrderByCustomer eq %27TEST%27)&$select=SalesSchedulingAgreement,PurchaseOrderByCustomer,Customer,CustomerName,SalesSchedulingAgreementItem,Material,MaterialByCustomer&$format=json

下面是我正在使用的查询程序。

我是不是漏掉了什么。请告诉我们 谢谢, 阿润派

final BatchRequestBuilder builder = BatchRequestBuilder.withService("/sap/opu/odata/sap/YY1_QUANTITYCONTRACTTRACKI_CDS");
    for (Contract contract : contracts) {
        FilterExpression mainFilter = new FilterExpression("CustomerName", "eq", ODataType.of(contract.getCustomerName()))
                .and(new FilterExpression("SalesSchedulingAgreement", "eq", ODataType.of(contract.getSchAgrmntNo())))
                .and(new FilterExpression("PurchaseOrderByCustomer", "eq", ODataType.of(contract.getCustRefNo())));
        final ODataQuery oDataQuery = ODataQueryBuilder
                .withEntity(sapConfig.getEssentialsContractServiceUrl(),
                        sapConfig.getEssentialsContractListEntity())
                .select("SalesSchedulingAgreement", "PurchaseOrderByCustomer", "Customer", "CustomerName",
                        "SalesSchedulingAgreementItem", "Material", "MaterialByCustomer")
                .filter(mainFilter)
                .build();
        builder.addQueryRequest(oDataQuery);
    }
    final BatchRequest batchRequest = builder.build();
    final BatchResult batchResult = batchRequest.execute(httpClient);

更新

我今天将版本更改为 3.35.0,连接版本为 1.40.11,但它也没有用。 下面是在控制台中打印的日志请求


2021-01-15 19:15:03.831 信息 42640 --- [io-8084-exec-10] c.s.c.s.o.c.impl.BatchRequestImpl : --batch_123 内容类型:application/http 内容传输编码:二进制

GET YY1_QuantityContractTracki?%24filter%3D%28%28CustomerName+eq+%2527Ford27%29+and+%28SalesSchedulingAgreement+eq+%25270030000141%2527%29%29+and+%28PurchaseOrderByCustomer+eq+%2527TEST%2527 %29%26%24select%3DSalesSchedulingAgreement%2CPurchaseOrderByCustomer%2CCustomer%2CCustomerName%2CSalesSchedulingAgreementItem%2CMaterial%2CMaterialByCustomer%26%24format%3Djson HTTP/1.1 接受:application/json;odata=verbose

--batch_123--

更新 (22.03.2021)

release of SAP Cloud SDK 3.41.0 this week we'll enable support for read operations in OData batch requests on the type-safe API. Please find the chapter in the respective documentation。 示例:

BusinessPartnerService service;
BusinessPartnerAddress addressToCreate1;
BusinessPartnerAddress addressToCreate2;

BusinessPartnerFluentHelper requestTenEntities = service.getAllBusinessPartner().top(10);
BusinessPartnerByKeyFluentHelper requestSingleEntity = service.getBusinessPartnerByKey("bupa9000");

BatchResponse result =
    service
        .batch()
        .addReadOperations(requestTenEntities)
        .addReadOperations(requestSingleEntity)
        .executeRequest(destination);

List<BusinessPartner> entities = result.getReadResult(requestTenEntities);
BusinessPartner entity = result.getReadResult(requestSingleEntity);

原回复:

我来自SAP Cloud SDK team. Generally we recommend our users to generate classes for their OData service interactions。通过这种方式,您可以轻松确保请求符合规范,同时注意类型安全。

很遗憾,我无法帮助您解决 BatchRequestBuilderBatchRequestBatchResult 的 API,因为它们不直接属于 SAP Cloud SDK 且不由我们维护。相反,我们建议我们自己的请求构建器。


如果上面链接的 类 的生成不适合您,那么我建议您试试我们的专家 API,其特点是 Generic OData Client SAP Cloud SDK。这是我们也将在内部用于我们生成的请求构建器的代码:

String servicePath = "/sap/opu/odata/sap/YY1_QUANTITYCONTRACTTRACKI_CDS";
ODataRequestBatch requestBatch = new ODataRequestBatch(servicePath, ODataProtocol.V2);
Map<Contract, ODataRequestRead> batchedRequests = new HashMap<>();

// iterate over contracts, construct OData query objects and add them to the OData batch request builder
for (Contract contract : contracts) {
    String entityName = sapConfig.getEssentialsContractListEntity();
    String serviceUrl = sapConfig.getEssentialsContractServiceUrl();
    StructuredQuery structuredQuery = StructuredQuery.onEntity(entityName, ODataProtocol.V2);

    structuredQuery.select("SalesSchedulingAgreement", "PurchaseOrderByCustomer", "Customer", "CustomerName", "SalesSchedulingAgreementItem", "Material", "MaterialByCustomer");
    structuredQuery.filter(FieldReference.of("SalesSchedulingAgreement").equalTo(contract.getSchAgrmntNo()));
    structuredQuery.filter(FieldReference.of("PurchaseOrderByCustomer").equalTo(contract.getCustRefNo()));

    String encodedQuery = structuredQuery.getEncodedQueryString();
    ODataRequestRead requestRead = new ODataRequestRead(serviceUrl, entityName, encodedQuery, ODataProtocol.V2);
    batchedRequests.put(contract, requestRead);
    requestBatch.addRead(requestRead);
}

// execute the OData batch request
ODataRequestResultMultipartGeneric batchResult = requestBatch.execute(httpClient);

// extract information from batch response, by referring to the individual OData request references
for( Map.Entry<Contract, ODataRequestRead> requestMapping : batchedRequests.entrySet() ) {
    ODataRequestResultGeneric queryResult = batchResult.getResult(requestMapping.getValue());
    List<Map<String, Object>> itemsForQuery = queryResult.asListOfMaps();
}

亲切的问候

亚历山大

供参考:同release of SAP Cloud SDK 3.41.0 we enabled support for read operations in OData batch requests on the type-safe API. Please find the chapter in the respective documentation. You would no longer need to use the Generic OData Client of SAP Cloud SDK as suggested in 。示例:

BusinessPartnerService service;
BusinessPartnerAddress addressToCreate1;
BusinessPartnerAddress addressToCreate2;

BusinessPartnerFluentHelper requestTenEntities = service.getAllBusinessPartner().top(10);
BusinessPartnerByKeyFluentHelper requestSingleEntity = service.getBusinessPartnerByKey("bupa9000");

BatchResponse result =
    service
        .batch()
        .addReadOperations(requestTenEntities)
        .addReadOperations(requestSingleEntity)
        .executeRequest(destination);

List<BusinessPartner> entities = result.getReadResult(requestTenEntities);
BusinessPartner entity = result.getReadResult(requestSingleEntity);