首页 > 解决方案 > 如何修复 SAP Cloud SDK 在应用程序启动期间引发的 DestinationAccessException

问题描述

我想用 Spring 应用程序扩展 S/4HANA 云系统。可以无错误地构建应用程序,也可以将其部署到 SAP Cloud Platform。当后端应用程序启动时,它会引发错误。日志中的错误如下:

[.../WEB-INF/classes/com/sap/controllers/ExportController.class]: Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'exportServiceImpl' defined in file [.../WEB-INF/classes/com/sap/services/ExportServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adsService' defined in class path resource [com/sap/ads/service/ServiceConfiguration.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.sap.ads.service.Service]: Factory method 'service' threw exception;
nested exception is com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException: Failed to get ConnectivityConfiguration: 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?

之后,似乎错误发生在带有DestinationAccessException的ServiceConfiguration.class中。

我已经检查并包含了这个: Creating ErpConfigContext throw exception

不幸的是,错误仍然相同。

以下是受影响部分的实现:

服务类

[...]
public interface Service {

    [...]

    public static final String DESTINATION_NAME = "myDestination";

    @RequestLine("POST /example/path")
    Response doSomething(Request myRequest);

}

服务配置类

[...]
@Configuration
public class ServiceConfiguration {

    @Bean
    public Service service() {
        return Feign.builder()
                .encoder(new JacksonEncoder())
                .decoder(new JacksonDecoder())
                .client(DestinationHelper.getHttpClient(Service.DESTINATION_NAME))
            .target(Service.class, DestinationHelper.getUrl(service.DESTINATION_NAME));
    }

}

DestinationHelper.class

[...]
public class DestinationHelper {

    /**
     * @return the URL of the destination with {@code destinationName}
     */
    public static String getUrl(String destinationName) {
        return DestinationAccessor
                .getDestination(destinationName)
                .getUri()
                .toString();
    }

    /**
     * @return an HTTP client preconfigured for the destination
     */
    public static ApacheHttpClient getHttpClient(final String destinationName) {
        return new ApacheHttpClient(HttpClientAccessor.getHttpClient(destinationName));
    }

}

服务实现中是否存在错误甚至遗漏?根据错误日志,该服务似乎无法真正建立与目标的正确连接。

标签: javasap-cloud-platformsap-cloud-sdks4hana

解决方案


这里的问题是,在应用程序启动期间,还没有RequestContext可用的。

应该解决的问题是将您的相关代码包装RequestContextExecutor如下:

@Bean
public Service service() throws Exception {
    return new RequestContextExecutor().execute(() -> {
        return Feign.builder()
            .encoder(new JacksonEncoder())
            .decoder(new JacksonDecoder())
            .client(DestinationHelper.getHttpClient(Service.DESTINATION_NAME))
        .target(Service.class, DestinationHelper.getUrl(service.DESTINATION_NAME));
    };
}

推荐阅读