我想用Spring应用程序扩展S/4HANA云系统。可以在没有任何错误的情况下构建应用程序,也可以将其部署到SAP云平台。当后端应用程序启动时,它会引发错误。日志中的错误如下:
[.../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投掷异常
不幸的是,错误仍然相同。
这是受影响部件的实现:
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));
}
}
服务实施中是否存在错误的实施甚至缺失?遵循错误日志,似乎该服务实际上无法建立与目的地的正确连接。
这里的问题是,在应用程序启动期间,尚无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));
};
}