Technical Articles
Destinations in SAP Business Technology Platform
SAP Business Technology Platform comes with lot of features to develop applications which can connect to different cloud and on-premise applications. In this blog, I will talk about one such feature – Destinations. Let’s see how destinations are configured and consumed in order to connect to remote systems.
Destinations contain basic information about how to connect to a system. Information can be a url, username, password or any customer headers.
Destination service is used to store and retrieve any technical information which is required to connect to a remote service from the application deployed in BTP.
Pre-requisites:
- Account in BTP(trial account can be used)
- Application deployed to BTP and SAP Cloud SDK added as it’s dependency(I am using JAVA application here)
1) Create a destination
There are multiple ways to create destinations:
- Using the Destinations Editor in the Cockpit
- Destination Service REST API
- Create Destinations Using the MTA Descriptor
- Create Destinations on Service Instance Creation
Let’s see first approach in this blog.
BTP cockpit -> Connectivity -> Destinations
Create Destination
With this destination configuration, “Northwind_Products” holds the basic information to connect to the public Products Service.
2) Create an instance of destination service
BTP cockpit > Service Marketplace -> search for destination -> Create
Instance Creation
Choose plan ‘lite’, give some instance name and click create
Service Plan Selection
If Destination service is not available in service marketplace, it can be added from BTP cockpit -> Entitlements -> Configure Entitlements -> Add Service Plans -> search for Destination -> Add.
3) Bind the service with your application
BTP cockpit > Instances -> Choose the instance -> Bind
Bind Application
Choose your application -> Create
Choose Application
Alternatively, application can be bound to a service using CF CLI or mta file as described here.
4) Consume the destination
By following the steps above, destination is configured in BTP cockpit and application is bound with destination service. Let’s see how to consume this destination using destination service in java code.
Ensure that the following SAP Cloud SDK is added as a dependency to the application. Pick the latest version from here.
<dependency>
<groupId>com.sap.cloud.sdk.cloudplatform</groupId>
<artifactId>scp-cf</artifactId>
<version>3.46.0</version>
</dependency>
Below is the sample REST controller, which has an endpoint to retrieve the data provided by remote service. Connection information is already configured with destination Northwind_Products. Here I am using DestinationAccessor provided by Cloud SDK to retrieve destination information with destination name.
package com.sap.renegotiation.sbo.controllers;
import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpClientAccessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination;
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException;
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationNotFoundException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@RestController
public class DestinationController {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@GetMapping("/getProducts")
public String getProducts() {
String DESTINATION_NAME = "Northwind_Products"; //in productive code, use constants file to
//retrieve destination name
try {
/* get the destination information from a facade provided by cloud platform */
HttpDestination destination =
DestinationAccessor.getDestination(DESTINATION_NAME).asHttp();
HttpClient client = HttpClientAccessor.getHttpClient(destination);
HttpResponse httpResponse = null;
try {
httpResponse = client.execute(new HttpGet());
/* Retrieve response data once the request is successful */
if (httpResponse.getStatusLine().getStatusCode() == 200) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpResponse.getEntity().getContent()));
StringBuilder result = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
return result.toString();
}
} catch (IOException e) {
log.info("IOException: " + e.getMessage());
return "IOException: " + e.getMessage();
}
} catch (DestinationNotFoundException e) {
/* Destination is not found with specified name */
log.info("DestinationNotFoundException: " + e.getMessage());
return "DestinationNotFoundException: " + e.getMessage();
} catch (DestinationAccessException e) {
/* Destination cannot be accessed */
log.info("DestinationAccessException: " + e.getMessage());
return "DestinationAccessException: " + e.getMessage();
}
return null;
}
}
Below is the response from application endpoint.
Response Data
Conclusion
To sum up, you have learnt how to configure and consume destinations in SAP Business Technology Platform. Same approach can be used to configure other connection details like authentication.
In case of any queries or feedback, feel free to post in comments.
Very helpful! Thank you so much, Soumya!
Hi Soumya,
Thanks for the good Blog.
Thanks and Regards,
Vijay.
Hi Soumya,
Thank you for this blog. It’s really helpful.
Can you please provide steps to add JWT token in request header.
Hi Gaurish,
Good to hear that. Destination takes care of adding token to the header, we just need to configure it with token retrieval details as shown below. Refer this for more authentication methods.
Destination Configuration