如何处理ODATA函数导入不返回包装元素



在我们的解决方案中,我们在S/4HANA侧上调用了基于自定义CDS的ODATA API,其中包括函数导入。为此,我们通过Maven-Plugin生成了ODATA接口的Java服务。调用函数导入基于BOPF,生成的实现不返回与SAP Cloud SDK的期望相匹配的结果,即结果JSON对象具有具有函数名称的成员。

我们检查了是否可以更改生成的ODATA实现,但在S/4HANA中没有发现配置。

我在拨打呼叫时已调试SDK实现,并在functionImportresponseparser中找到以下代码,这使此假设:

    <T> T getEntityFromResponse(
        final InputStream responseContent,
        final String edmFunctionImportName,
        final Class<? extends T> entityJavaType )
        throws IOException,
            IllegalArgumentException
    {
        final JsonObject responseJsonObject = getJsonObjectFromResponse(responseContent);
(X)     if( responseJsonObject.has(edmFunctionImportName) ) {
            final JsonElement jsonElement = responseJsonObject.get(edmFunctionImportName);
            return getEntityFromJsonElement(jsonElement, entityJavaType);
        }
        return null;
    }

由于以(x(为标记的if statement以及该函数导入直接返回对象未包装在预期成员中的事实,结果null返回。

因此,对我来说,Queston是,如果SDK也可以处理此情况,或者该错误是否在S/4HANA API中不返回ODATA符合结果。

不幸的是,消耗API时没有简单的方法来自定义此行为。正如您已经注意到的那样,当前实现期望JSON结果包含一个与函数导入 import import 名称相同的对象。

中调用。

这是您问题的临时解决方法:

  • 覆盖 fluenthperfunction 实现类,具有以下" getFunctionName"方法:

    @Override
    @Nonnull
    protected String getFunctionName()
    {
        final String callingMethod = Thread.currentThread().getStackTrace()[2].getMethodName();
        if( "generatePath".equals(callingMethod) ) {
            return "TheFunctionNameInUrlPath";
        }
        if( "executeSingle".equals(callingMethod) ) {
            return "TheKeyOfODataResponse";
        }
        throw new IllegalStateException("This should not happen.");
    }
    

虽然这不是很好的代码,但我们将通过SAP Cloud SDK检查如何使其更容易。


更新:不幸的是,您正在收到的JSON响应并未将内容包装到根元素" D"的另一个对象中。这使得问题更加困难。

如果您仍然想在这种情况下使用 sap cloud SDK ,则需要调整一些内部代码。而不是上面的getFunctionName列表,请尝试以下更改execute(或executeSingle(方法:

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import com.google.common.io.CharStreams;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.json.JsonSanitizer;
import com.sap.cloud.sdk.odatav2.connectivity.ODataExceptionType;
import com.sap.cloud.sdk.odatav2.connectivity.ODataGsonBuilder;
import com.sap.cloud.sdk.result.GsonResultElementFactory;
import com.sap.cloud.sdk.result.ResultElement;
import org.apache.http.HttpEntity;
...
    @Override
    @Nullable
    public T execute( @Nonnull final ErpConfigContext configContext )
        throws ODataException
    {
        final HttpEntity httpEntity = accessibleQuery(configContext);
        final ProposalHeader response;
        try( final InputStream content = httpEntity.getContent() ) {
            final String rawContent = CharStreams.toString(new InputStreamReader(content, StandardCharsets.UTF_8));
            final JsonElement responseJsonElement = new JsonParser().parse(JsonSanitizer.sanitize(rawContent));
            // select JSON root object
            final JsonElement jsonElement = responseJsonElement.getAsJsonObject().getAsJsonObject("d");
            // deserialize contents
            final GsonResultElementFactory elementFactory = new GsonResultElementFactory(ODataGsonBuilder.newGsonBuilder());
            final ResultElement resultElement = elementFactory.create(jsonElement);
            response = resultElement.getAsObject().as(getEntityClass());
        }
        catch( final IOException | IllegalArgumentException | JsonSyntaxException e ) {
            throw new ODataException(ODataExceptionType.ODATA_OPERATION_EXECUTION_FAILED, "Failed to read OData result.", e);
        }
        return response;
    }
    private HttpEntity accessibleQuery( @Nonnull final ErpConfigContext configContext ) {
        try {
            final Method query = FluentHelperFunction.class.getDeclaredMethod("query", ErpConfigContext.class);
            query.setAccessible(true);
            Object httpEntityRaw = query.invoke(this, configContext);
            return HttpEntity.class.cast(httpEntityRaw);
        }
        catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassCastException e) {
            // log error
            // throw exception
            return null;
        }
    }

您可以将通用类型的T更改为预期响应类。还要更改// log error// throw exception的行以匹配您的应用程序用例,以便将来可以轻松完成错误处理。此外,您应该向此代码介绍一些null检查。

最新更新