sap-erpsap-cloud-sdkbapi

Calling BAPI from Java using SAP Cloud SDK version 3.0


I am working on a POC currently where I need to call a BAPI inside S/4HANA On-Premise from a cloud application using Java. For this I am using SAP Cloud SDK.

For initial setup I followed these steps: https://developers.sap.com/tutorials/s4sdk-setup.html

I am trying to call BAPI_COSTCENTER_GETLIST1 from my application. Here is the code snippet:

final BapiRequestResult resultGetCostCenters = new BapiRequest("BAPI_COSTCENTER_GETLIST1")
 .withExporting("CONTROLLINGAREA", "BAPI0012_GEN-CO_AREA", "0001")
 .withTable("COSTCENTERLIST", "BAPI0012_CCLIST").end()
 .withTableAsReturn("BAPIRET2")
 .execute(destination);

And here the declaration of destination variable:

Map<String, String> destinationProps = new HashMap<String, String>();
// Added a sample destination name. Solved NoSuchElementException error. 
destinationProps.put("Name", "SampleDestination");
destinationProps.put("URL", "<system_url>");
destinationProps.put("Type", "HTTP");
destinationProps.put("Password", "<system_password>");
destinationProps.put("Authentication", "BasicAuthentication");
destinationProps.put("User", "<system_user>");

final Destination destination= new DefaultDestination(destinationProps);

But from execute(), one exception in being thrown which says

java.util.NoSuchElementException: No value present

Here is the complete stack trace

java.util.NoSuchElementException: No value present
    at io.vavr.control.Option$None.get(Option.java:526)
    at com.sap.cloud.sdk.s4hana.connectivity.rfc.SoapTransaction.before(SoapTransaction.java:56)
    at com.sap.cloud.sdk.s4hana.connectivity.rfc.RemoteFunctionRequestExecutor.execute(RemoteFunctionRequestExecutor.java:43)
    at com.sap.cloud.sdk.s4hana.connectivity.rfc.BapiRequest.execute(BapiRequest.java:78)
    at com.sap.csc.CostCenterCreationServlet.retrieveCostCenterList(CostCenterCreationServlet.java:73)
    at com.sap.csc.CostCenterCreationServlet.doGet(CostCenterCreationServlet.java:39)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.sap.cloud.sdk.cloudplatform.servlet.RequestAccessorFilter.lambda$doFilter$1(RequestAccessorFilter.java:71)
    at com.sap.cloud.sdk.cloudplatform.thread.AbstractThreadContextExecutor.lambda$execute$0(AbstractThreadContextExecutor.java:317)
    at com.sap.cloud.sdk.cloudplatform.thread.ThreadContextCallable.call(ThreadContextCallable.java:247)
    at com.sap.cloud.sdk.cloudplatform.thread.AbstractThreadContextExecutor.execute(AbstractThreadContextExecutor.java:319)
    at com.sap.cloud.sdk.cloudplatform.servlet.RequestAccessorFilter.doFilter(RequestAccessorFilter.java:71)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.sap.cloud.sdk.cloudplatform.security.servlet.HttpCachingHeaderFilter.doFilter(HttpCachingHeaderFilter.java:80)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.sap.cloud.sdk.cloudplatform.security.servlet.HttpSecurityHeadersFilter.doFilter(HttpSecurityHeadersFilter.java:41)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.filters.RestCsrfPreventionFilter.doFilter(RestCsrfPreventionFilter.java:113)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:835)

I assume I am missing something very basic in it. Or probably I am doing it completely wrong. I could not find any well documented tutorial on this topic. So please correct me where I am going wrong.

Thanks in advance.

(SAP Cloud SDK Overview Page - https://help.sap.com/doc/0786ec6134c04c9bbafec959b6b737de/1.0/en-US/index.html)

Complete stack trace for Target host is not specified exception

com.sap.cloud.sdk.s4hana.connectivity.exception.RequestExecutionException: BapiRequest com.sap.csc.CostCenterCreationServlet.retrieveCostCenterList(CostCenterCreationServlet.java:69) failed [total: N/A, build payload: 0.243255s, execute request: N/A, parse response: N/A]
com.sap.cloud.sdk.s4hana.connectivity.rfc.exception.RemoteFunctionException: com.sap.cloud.sdk.s4hana.connectivity.exception.RequestExecutionException: BapiRequest com.sap.csc.CostCenterCreationServlet.retrieveCostCenterList(CostCenterCreationServlet.java:69) failed [total: N/A, build payload: 0.243255s, execute request: N/A, parse response: N/A]
    at com.sap.cloud.sdk.s4hana.connectivity.rfc.SoapTransaction.execute(SoapTransaction.java:70)
    at com.sap.cloud.sdk.s4hana.connectivity.rfc.RemoteFunctionRequestExecutor.execute(RemoteFunctionRequestExecutor.java:46)
    at com.sap.cloud.sdk.s4hana.connectivity.rfc.BapiRequest.execute(BapiRequest.java:78)
    at com.sap.csc.CostCenterCreationServlet.retrieveCostCenterList(CostCenterCreationServlet.java:73)
    at com.sap.csc.CostCenterCreationServlet.doGet(CostCenterCreationServlet.java:39)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.sap.cloud.sdk.cloudplatform.servlet.RequestAccessorFilter.lambda$doFilter$1(RequestAccessorFilter.java:71)
    at com.sap.cloud.sdk.cloudplatform.thread.AbstractThreadContextExecutor.lambda$execute$0(AbstractThreadContextExecutor.java:317)
    at com.sap.cloud.sdk.cloudplatform.thread.ThreadContextCallable.call(ThreadContextCallable.java:247)
    at com.sap.cloud.sdk.cloudplatform.thread.AbstractThreadContextExecutor.execute(AbstractThreadContextExecutor.java:319)
    at com.sap.cloud.sdk.cloudplatform.servlet.RequestAccessorFilter.doFilter(RequestAccessorFilter.java:71)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.sap.cloud.sdk.cloudplatform.security.servlet.HttpCachingHeaderFilter.doFilter(HttpCachingHeaderFilter.java:80)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.sap.cloud.sdk.cloudplatform.security.servlet.HttpSecurityHeadersFilter.doFilter(HttpSecurityHeadersFilter.java:41)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.filters.RestCsrfPreventionFilter.doFilter(RestCsrfPreventionFilter.java:113)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: com.sap.cloud.sdk.s4hana.connectivity.exception.RequestExecutionException: BapiRequest com.sap.csc.CostCenterCreationServlet.retrieveCostCenterList(CostCenterCreationServlet.java:69) failed [total: N/A, build payload: 0.243255s, execute request: N/A, parse response: N/A]
    at com.sap.cloud.sdk.s4hana.connectivity.ErpHttpRequestExecutor.execute(ErpHttpRequestExecutor.java:662)
    at com.sap.cloud.sdk.s4hana.connectivity.ErpHttpRequestExecutor.execute(ErpHttpRequestExecutor.java:410)
    at com.sap.cloud.sdk.s4hana.connectivity.rfc.SoapTransaction.execute(SoapTransaction.java:65)
    ... 44 more
Caused by: org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:187)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at com.sap.cloud.sdk.cloudplatform.connectivity.HttpClientWrapper.execute(HttpClientWrapper.java:94)
    at com.sap.cloud.sdk.s4hana.connectivity.ErpHttpRequestExecutor.execute(ErpHttpRequestExecutor.java:646)
    ... 46 more
Caused by: org.apache.http.ProtocolException: Target host is not specified
    at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:71)
    at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:125)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)

Solution

  • Please provide "Name" entry in your destination properties, e.g.

    destinationProps.put("Name", "<some_name>");