簡體   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