在JavaEE for SAP云平台中使用目的地

在JavaEE for SAP云平台中使用目的地,java,servlets,sap-cloud-platform,Java,Servlets,Sap Cloud Platform,第一次尝试: HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); URL是硬编码字符串。通过这种方式,应用程序可以使用Tomcat服务器在本地工作,但在SAP云平台上运行时则不能。在SCP上,它将导致由以下原因引起的:java.net.ConnectException:连接超时(连接超时)(本地端口53603到地址0.0.0.0,远程端口443到地址xxx.xxx

第一次尝试:

HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
URL是硬编码字符串。通过这种方式,应用程序可以使用Tomcat服务器在本地工作,但在SAP云平台上运行时则不能。在SCP上,它将导致由以下原因引起的
:java.net.ConnectException:连接超时(连接超时)(本地端口53603到地址0.0.0.0,远程端口443到地址xxx.xxx.xxx

第二次尝试:

    Context ctx = new InitialContext();
    HttpDestination destination = (HttpDestination) ctx.lookup("java:comp/env/myDestination");
    HttpClient client = destination.createHttpClient();
使用web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>myApp</display-name>
  <resource-ref>
    <res-ref-name>myDestination</res-ref-name>
    <res-type>com.sap.core.connectivity.api.http.HttpDestination</res-type>
</resource-ref>
</web-app>
仅供参考-在Eclipse中使用neo-java-web-sdk-3.66.4.1

您不能在servlet中为HTTPClient使用硬编码URL吗? 如何解决
com.sap.core.connectivity.api.http.HttpDestination
参考问题?
如果代码正在使用目的地,您仍然可以在本地测试应用程序吗?

我强烈建议使用。它是一个SDK,通过为所有云平台机制提供易于使用的机制,为SAP云平台构建应用程序而开发

关于您手头的任务,有一个
DestinationAccessor
类,您可以这样使用:

DestinationAccessor.getDestination("MyDestinationName");
假设您已经在云驾驶舱的空间中配置了一个目的地,这将解析该目的地,并让您将其传递到代码的其他部分。这在Neo和cloud Foundry上都有效

如果这听起来能解决你的问题,我建议你先去看看

或者,您也可以简单地将以下依赖项添加到项目中,以开始测试SDK:

<dependency>
    <groupId>com.sap.cloud.s4hana.cloudplatform</groupId>
    <artifactId>scp-neo</artifactId>
    <version>2.7.0</version>
</dependency>

com.sap.cloud.s4hana.cloudplatform
近地天体
2.7.0
对于Cloud Foundry,请使用
scp cf
而不是
scp neo

希望这有帮助

<dependency>
    <groupId>com.sap.cloud.s4hana.cloudplatform</groupId>
    <artifactId>scp-neo</artifactId>
    <version>2.7.0</version>
</dependency>