创建 ErpConfigContext 抛出异常

Creating ErpConfigContext threw exception

我正在尝试使用 S/4HANA SDK 调用 CompanyCode OData VDM 服务,但如果我在 Cloud Foundry 环境中执行此操作,则会收到附加错误。 我之前一直在使用 SDK,但这并没有发生。你能帮我一下吗?

代码:

public List<CompanyCode> findAll() {
    try {
        return compCodeService.getAllCompanyCode().execute(erp())
                .stream()
                .map(this::toCompCode)
                .collect(Collectors.toList());
    } catch (ODataException e) {
        throw SapException.create(e.getMessage());
    }
}

private ErpConfigContext erp(){
    return new ErpConfigContext(REPLICATE_WORKFORCE);
}

 public static final String REPLICATE_WORKFORCE = "REPLICATE_WORKFORCE";

错误:

 [Request processing failed; nested exception is com.sap.cloud.sdk.cloudplatform.tenant.exception.TenantAccessException: Failed to get current tenant: no RequestContext available. Have you correctly configured a RequestContextServletFilter or have you wrapped your logic in a RequestContextExecutor when executing background tasks that are not triggered by a request?] with root cause
    com.sap.cloud.sdk.cloudplatform.tenant.exception.TenantAccessException: Failed to get current tenant: no RequestContext available. Have you correctly configured a RequestContextServletFilter or have you wrapped your logic in a RequestContextExecutor when executing background tasks that are not triggered by a request?
   at com.sap.cloud.sdk.cloudplatform.tenant.AbstractTenantFacade.getCurrentTenantIfAvailable(AbstractTenantFacade.java:85) ~[tenant-2.5.0.jar!/:na]

   at com.sap.cloud.sdk.cloudplatform.tenant.TenantAccessor.getCurrentTenantIfAvailable(TenantAccessor.java:109) ~[tenant-2.5.0.jar!/:na]
   at com.sap.cloud.sdk.cloudplatform.connectivity.ScpCfDestinationFacade.getCacheKey(ScpCfDestinationFacade.java:132) ~[connectivity-scp-cf-2.5.0.jar!/:na]
   at com.sap.cloud.sdk.cloudplatform.connectivity.ScpCfDestinationFacade.getGenericDestinationsByName(ScpCfDestinationFacade.java:178) ~[connectivity-scp-cf-2.5.0.jar!/:na]
   at com.sap.cloud.sdk.cloudplatform.connectivity.AbstractDestinationFacade.getGenericDestination(AbstractDestinationFacade.java:173) ~[com.sap.cloud.s4hana.cloudplatform-connectivity-2.5.0.jar!/:na]
   at com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor.getGenericDestination(DestinationAccessor.java:176) ~[com.sap.cloud.s4hana.cloudplatform-connectivity-2.5.0.jar!/:na]
   at com.sap.cloud.sdk.s4hana.connectivity.ErpConfigContext.(ErpConfigContext.java:218) ~[com.sap.cloud.s4hana-connectivity-2.5.0.jar!/:na]
   at com.sap.cloud.sdk.s4hana.connectivity.ErpConfigContext.(ErpConfigContext.java:367) ~[com.sap.cloud.s4hana-connectivity-2.5.0.jar!/:na]
   at com.sap.cloud.sdk.s4hana.connectivity.ErpConfigContext.(ErpConfigContext.java:442) ~[com.sap.cloud.s4hana-connectivity-2.5.0.jar!/:na]
   at com.sap.csc.employeecreationbe.repository.SdkCompanyCodeRepository.erp(SdkCompanyCodeRepository.java:74) ~[classes/:na]
   at com.sap.csc.employeecreationbe.repository.SdkCompanyCodeRepository.findAll(SdkCompanyCodeRepository.java:64) ~[classes/:na]
   at com.sap.csc.employeecreationbe.repository.SdkCompanyCodeRepository$$FastClassBySpringCGLIB$$f89ad4b4.invoke() ~[classes/:na]
   at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
   at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) ~[spring-aop-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
   at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
   at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
   at com.sap.csc.employeecreationbe.repository.SdkCompanyCodeRepository$$EnhancerBySpringCGLIB$dd15cde.findAll() ~[classes/:na]
   at com.sap.csc.employeecreationbe.controller.EmployeeController.companyCodes(EmployeeController.java:216) ~[classes/:na]
   at com.sap.csc.employeecreationbe.controller.EmployeeController$$FastClassBySpringCGLIB$3220b2.invoke() ~[classes/:na]
   at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
   at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) ~[spring-aop-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
   at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
   at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.8.RELEASE.jar!/:5.0.8.RELEASE] and so on

您能否尝试确保 RequestContextServletFilter 在您的应用程序中被选中? RequestContextServletFilter 默认使用 @WebFilter 注解。

Spring 引导示例:

@SpringBootApplication
@ServletComponentScan( "com.sap.cloud.sdk" )
public class Application extends SpringBootServletInitializer
{
    ...
}

另请参阅:https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/ServletComponentScan.html

编辑: 请注意,当 运行 你的代码在后台作业(不是由请求触发)时,你必须用 RequestContextExecutor::execute.

包装你的代码