I want to extend a S/4HANA Cloud System with a Spring application. It is possible to build the application without any error, and it can be also deployed to the SAP Cloud Platform. While the Backend application is starting, it throws a error. The error from the Logs is as follows:
[.../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?
Following that, it seems the error happen in the ServiceConfiguration.class with the DestinationAccessException.
I have already checked and included this: Creating ErpConfigContext threw exception
Unfortunately the error is still the same.
Here is the implementation of the affected parts:
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));
}
}
Is there something wrong implemented or even missing in the Service implementation? Following the error log it seems that the service is not really able to establish a proper connection to the Destination.
The issue here is that during start-up of the application, no RequestContext
is available yet.
What should fix the issue is wrapping your relevant code in a RequestContextExecutor
as follows:
@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));
};
}