Odata 查询批处理请求 - 过滤器不起作用

问题描述

我们正在使用 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",ODataType.of(contract.getSchAgrmntNo())))
                .and(new FilterExpression("PurchaSEOrderByCustomer",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 INFO 42640 --- [io-8084-exec-10] c.s.c.s.o.c.impl.BatchRequestImpl : --batch_123 内容类型:应用程序/http 内容传输编码:二进制

GET YY1_QuantityContractTracki?%24filter%3D%28%28CustomerName+eq+%2527Ford27%29+and+%28SalesSchedulingAgreement+eq+%25270030000141%2527%29%29+and+%29%29+chase+%29%29%28Pur5%29%28Pur5%29%28Pur5 %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,我们将启用对类型安全 API 上 OData 批处理请求中的读取操作的支持。请在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 团队。通常,我们建议我们的用户使用 generate classes for their OData service interactions。通过这种方式,您可以轻松确保请求符合规范,同时兼顾类型安全。

很遗憾,我无法帮助您处理 BatchRequestBuilderBatchRequestBatchResult 的 API,因为它们不是 SAP Cloud SDK 的直接一部分,也没有得到维护被我们。相反,我们建议使用我们自己的请求构建器。


如果上面链接的类的生成不适合您,那么我建议您尝试我们的专家 API,该 API 具有 SAP Cloud SDK通用 OData 客户端 em>。这是我们也将在内部用于生成的请求构建器的代码:

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,我们启用了对类型安全 API 上 OData 批处理请求中的读取操作的支持。请在the respective documentation中找到章节。您将不再需要按照 the other response 中的建议使用 SAP Cloud SDKGeneric OData Client。示例:

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);