简体   繁体   中英

Using destinations in Java EE for SAP Cloud Platform

First try:

HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);

The URL is a hardcoded String. This way the application works locally using the Tomcat server, but not when running on SAP Cloud Platform. On SCP it will result in Caused by: java.net.ConnectException: Connection timed out (Connection timed out) (local port 53603 to address 0.0.0.0, remote port 443 to address xxx.xxx.xxx.xxx .

Second try:

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

using 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>

In this case the import com.sap.core.connectivity.api.http.HttpDestination;

cannot be resolved and when I run in ton SCP it shows similar errors:
with root cause java.lang.Error: Unresolved compilation problems: 
HttpDestination cannot be resolved to a type
HttpDestination cannot be resolved to a type

FYI - Using neo-java-web-sdk-3.66.4.1 in Eclipse Oxygen.

Can you not use hardcoded URLs for an HTTPClient in a servlet? How do I solve the com.sap.core.connectivity.api.http.HttpDestination reference issue? Can you still test an application locally if the code is using destinations?

I highly suggest using the SAP S/4HANA Cloud SDK for such tasks. It is an SDK developed to make building applications for SAP Cloud Platform easy, by providing easy to use mechanisms for all the Cloud Platform mechanisms.

Regarding your task at hand, there is a DestinationAccessor class that you can use like this:

DestinationAccessor.getDestination("MyDestinationName");

Given that you have configured a destination in your space in the cloud cockpit, this will resolve that destination and let's you pass it on to other parts of your code. This works both on Neo and on Cloud Foundry.

If this sounds like it could solve your problem, I recommend checking out this blog post series to get started.

Alternatively, you can also simply add the following dependency to your project to start testing the SDK:

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

For Cloud Foundry use scp-cf instead of scp-neo .

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM