如何修复 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 中。
我已经检查并包含了这个:
不幸的是,错误仍然存在。
以下是受影响部分的实施:
Service.class:
[...]
public interface Service {
[...]
public static final String DESTINATION_NAME = "myDestination";
@RequestLine("POST /example/path")
Response doSomething(Request myRequest);
}
ServiceConfiguration.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));
}
}
服务实现中是否存在错误甚至缺失?根据错误日志,该服务似乎无法真正建立到目标的正确连接。
这里的问题是在申请 start-up 期间,还没有 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));
};
}
我想用 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 中。
我已经检查并包含了这个:
不幸的是,错误仍然存在。
以下是受影响部分的实施:
Service.class:
[...]
public interface Service {
[...]
public static final String DESTINATION_NAME = "myDestination";
@RequestLine("POST /example/path")
Response doSomething(Request myRequest);
}
ServiceConfiguration.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));
}
}
服务实现中是否存在错误甚至缺失?根据错误日志,该服务似乎无法真正建立到目标的正确连接。
这里的问题是在申请 start-up 期间,还没有 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));
};
}