问题
Using LATEST java SAP Cloud Sdk
We are trying to perform an update to OutboundDelivery in S/4 system. We are using API_OUTBOUND_DELIVERY_SRV version 2 for it. The service requires us to use etag, i.e. it requires us to provide a header - if-match
with the corresponding value of the etag.
We are using VDM for performing the update and are making use of com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.outbounddeliveryv2.batch.OutboundDeliveryV2ServiceBatch
. Please find the code snippet used for update, below:
...
OutboundDeliveryV2ServiceBatch service = // Instantiate service;
List<OutbDeliverItem> itemsToUpdate = new ArrayList<>();
items.add(OutbDeliveryItem.builder()
.deliveryDocument("some key")
.deliveryDocumentItem("some key")
.build());
// Assume more additions to items
...
...
...
OutboundDeliveryV2ServiceBatchChangeSet changeSet = service.beginChangeSet();
items.forEach(changeSet::updateOutbDeliveryItem);
changeSet.endChangeSet();
BatchResponse response = service.execute(someDestination);
...
As soon as service.execute(someDestination)
is executed, the update is not performed on S/4 and the logs on S/4 give following error:
The Data Service Request is required to be conditional. Try using the "If-Match" header.
My questions:
- Why does the VDM/SDK not take care of etag handling internally? According to the section Optimistic concurrency control in this blog, it is mentioned that it is taken care of automatically in javascript sdk but what about java sdk?
- Is it possible to pass this header by using VDM somehow? If yes then how?
- Or do we have to let go of the optimistic concurrency control from our service in S/4?
Please note that we have to use the batch operation itself due to performance reasons.
回答1:
The ETags are missing because the items to be updated are programmatically created in your application using the builder pattern. Therefore, no ETags are present on them when you execute update and the SDK has none available to set the "If-Match" header when building up the request.
The solution is to fetch the items from the server first. This also ensures that you have the latest version of them for the update which is a prerequisite for updating them.
回答2:
First of all, I'm confused with regard to which version of the SAP Cloud SDK you're using. Your mentioning JavaScript in your first question, but com.sap.cloud.sdk.s4hana
is a Maven package identifier, i.e. Java. Can you please clarify which version you're using?
Either by, both versions should indeed handle this automatically for you. I'm assuming that you're trying to perform some kind of write request (UPDATE or DELETE) that produces the error message you've posted here. In order to send an eTag with such a request, the current eTag must be known beforehand, of course. Have you fetched the latest state of the entity before performing your write request?
Finally, the JavaScript SDK allows you to ignore version identifiers with the ignoreVersionIdentifier
method on the respective request builder. Generally this is not recommended of course! So make sure to understand why an eTag is required before deciding whether you can ignore it. Finally, there's also withCustomHeader
where you can supply your own eTag header if for whatever reason none of the above approaches work for you.
来源:https://stackoverflow.com/questions/60055957/e-tag-handling-for-odatav2-in-sap-cloud-sdk-vdm