繁体   English   中英

如何修复应用程序启动期间 SAP Cloud SDK 抛出的 DestinationAccessException

[英]How to fix a DestinationAccessException thrown by the SAP Cloud SDK during application startup

我想使用 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?

在此之后,似乎在ServiceConfiguration.class 中发生了DestinationAccessException错误。

我已经检查并包含了这个: 创建 ErpConfigContext 抛出异常

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

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

服务类

[...]
public interface Service {

    [...]

    public static final String DESTINATION_NAME = "myDestination";

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

}

服务配置.class :

[...]
@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));
    }

}

服务实施中是否有错误实施甚至缺失? 根据错误日志,该服务似乎无法真正与目标建立正确的连接。

这里的问题是在应用程序启动期间,还没有可用的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));
    };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM