Integrate SAP S/4HANA Cloud SDK into your Java application

The following steps will explain how to include the SAP S/4HANA Cloud SDK in existing Java web application projects.

Note: This post is part of a series. For a complete overview visit the SAP S/4HANA Cloud SDK Overview.

Goal of this blog post

The goal of this post is to show the various options for a developer to include the public libraries to his newly created or already existing project.

* Software requirements and recommendations.
* Best practice for enabling SAP S/4HANA Cloud SDK
* Head start with Maven archetypes
* Library functionalities and software artifacts
* Example: Spring Boot

This blog post exists as part of general help articles outside of the continuous course starting from  Step 1 (Setup). We highly recommend following this series in order to understand feature scope and expected usability of the SAP S/4HANA Cloud SDK.

Software requirements and recommendations

The compiled libraries of SAP S/4HANA Cloud SDK are publicly available and can be used in any Java web application which uses the Java Servlet API. The following properties may be applicable to your software project.

Required:

* Java Runtime version 1.7+
* Maven, Java dependency management tool to easily include SAP S/4Hana Cloud SDK artifacts. It dynamically downloads any required Java libraries to realize the build process.

Recommended:

* Multi module project setup for your software, to ensure a consistent application development structure.
* Supported Servlet 3.0 API to have all features activated automatically during application startup. It allows the web application to be configured by Java annotations.

Best practice

The SAP S/4HANA Cloud SDK provides an extensive set of tools to enable, organize and secure the communication between your web application and your ERP system. These tools are publicly provided as Maven artifacts.

How to start

* Add an entry to your dependencyManagement of your application POM. In case you have a multi-module Maven setup in place, please consider adding this to the parent module. This way inconsistencies and redundancies in your software can be avoided. … com.sap.cloud.s4hana sdk-bom LATEST pom import … ​

Now you can easily include any dependency of the SAP S/4HANA Cloud SDK to your respective Maven module. Specifying a dependency version is highly recommended, because LATEST could bring incompatibilities sometime in the future.
* Depending on your target deployment platform, you can choose between CloudFoundry (scp-cf) and Neo (scp-neo). Since you added the dependency import previously, you can omit any further version declaration of the SAP S/4HANA Cloud SDK. Notice the different artifact identifier on the first dependency each:

* CloudFoundry (scp-cf) … com.sap.cloud.s4hana.cloudplatform scp-cf com.sap.cloud.s4hana s4hana-all … … ​
* Neo (scp-neo) … com.sap.cloud.s4hana.cloudplatform scp-neo com.sap.cloud.s4hana s4hana-all … … ​

Unit tests and integration tests

For testing an application, the SAP S/4HANA Cloud SDK provides a test utility, which enables you to access real and mocked ERP systems. The recommended way is to have one or more dedicated modules for unit tests and integration tests.

* Each can have the following dependencies attached: … com.sap.cloud.s4hana testutil test com.sap.cloud.s4hana.quality listeners-all test … … ​

Head start with Maven archetypes

Boost your project building process with Maven archetypes!

When you consider starting a new project or migrating an already existing one, please find the SAP S/4HANA Cloud SDK archetypes. With these you can easily create a new Maven project and migrate your existing files to a clean environment. Currently there are four supported archetypes for different runtimes; three of them with a well-established CloudFoundry support.

Spring Boot

In case your application is or will be based on Spring Boot, we highly recommend using the dedicated Spring archetype which has already been prepared for SAP S/4HANA Cloud SDK. In an empty location run the following command: mvn archetype:generate -DarchetypeGroupId=com.sap.cloud.s4hana.archetypes -DarchetypeArtifactId=scp-cf-spring -DarchetypeVersion=LATEST

You will be asked for groupId, artifactId, package and uniqueHostname. The last one represents a unique identifier to determine your initial project URL on CloudFoundry. It can be easily changed later. The project stub which is generated from the archetype contains the best effort project architecture with most of Cloud SDK features already in place. It is a quick start to a deployable web application.

* Easy debugging, e.g. with executing main()
* Run application locally with mvn spring-boot:run
* Run packaged application anywhere with java -jar

Apache Tomee

If your application requires a Java Enterprise Edition of Apache Tomcat, you can use the following archetype: mvn archetype:generate -DarchetypeGroupId=com.sap.cloud.s4hana.archetypes -DarchetypeArtifactId=scp-cf-tomee -DarchetypeVersion=LATEST

* Run application locally with mvn package tomee:run
* Debug with remote debugging to localhost

Apache Tomcat

Probably the most popular application, to run Servlet based application, is Tomcat. You can create a new project stub with Tomcat support by running the following command: mvn archetype:generate -DarchetypeGroupId=com.sap.cloud.s4hana.archetypes -DarchetypeArtifactId=scp-cf-tomcat -DarchetypeVersion=LATEST

* Run application locally with mvn jetty:run
* Debug with remote debugging to localhost

Java EE6

If your target application platform is Neo, then you would need to create the software stub with the following archetype: mvn archetype:generate -DarchetypeGroupId=com.sap.cloud.s4hana.archetypes -DarchetypeArtifactId=scp-neo-javaee6 -DarchetypeVersion=LATEST

* Deploy and run application locally with mvn package scp:push
* Stop local application with mvn clean scp:clean
* Debug with remote debugging to localhost

Functionalities and library artifacts

The functionality of SAP S/4HANA Cloud SDK is provided by Maven artifacts. By specifying dependencies in your application, you can take advantage of most of its features. When including “s4hana-all” like shown before, you already have them attached. Most of the feature bearing artifacts are distributed by the following Maven artifacts:

Feature
Description
Maven Group ID
Maven Artifact ID

S/4HANA
Bundle of all S/4HANA modules, including OData V2 connectivity, can be used on both Neo and Cloud Foundry
com.sap.cloud.s4hana
s4hana-all

S/4HANA – Core
Basic functionality for S/4HANA such as data types and serialization
com.sap.cloud.s4hana
core

S/4HANA – Connectivity
Establish and manage connection to S/4HANA using customer-supplied information via Cloud Platform abstractions. Includes security, caching, resilience.
com.sap.cloud.s4hana
connectivity

S/4HANA – Data Model
Typed data model of OData and BAPI services (as exposed by S/4HANA Cloud), allows easy access via a fluent business object-oriented API.
com.sap.cloud.s4hana
datamodel

S/4HANA – Fluent Result API
Fluent interface for handling various types of query results, including type conversion components.
com.sap.cloud.s4hana
fluent-result

S/4HANA – RFC Queries
Call BAPIs / RFMs in S/4HANA easily with a high level of abstraction.
com.sap.cloud.s4hana
rfc

S/4HANA – Test Utilities
Utility classes for testing ERP-related functionality, also for easy mocking of Cloud Platform entities in general.
com.sap.cloud.s4hana
testutil

Please notice, that s4hana-all includes all the other artifacts from the table, except for testutil. The test utility should only be added to testing modules and environments. To enable the S/4HANA communication, please remember to also include either “scp-cf” or “scp-neo” from group “com.sap.cloud.s4hana.cloudplatform”.

Project examples

Good to know: If your software already relies on a set of dependency imports, it becomes important to structure the import order accordingly. The entries are enumerated with decreasing priority. Please find the example for a parent POM below: … com.sap.cloud.s4hana sdk-bom LATEST pom import …

As you can see, the original dependency versions given by your application will be in favor of the ones given by SAP S/4HANA Cloud SDK. To solve a potential Maven dependency conflict, you can also specify a preferred version above. This will enforce specific version numbers, if none is provided.

Spring Boot

If your software is built with the Spring Boot framework, make sure to also have the dependency spring-boot-starter-web from group “org.springframework.boot” included. Otherwise the required ServletContext will not be accessible internally. You can find a sophisticated Spring Boot example in our public repository: The Costcenter Controller for CloudFoundry. It follows our best effort principles.

The first thing to do, is to include the dependencyManagement entry to your Maven root POM just like it was showed above. Now it could look like this: … org.json json 20170516 org.mockito mockito-core 2.10.0 org.springframework.boot spring-boot-dependencies 1.5.7.RELEASE pom import com.sap.cloud.s4hana sdk-bom LATEST pom import …

A dependency version will be determined by the priority, the order in which dependency imports are enumerated. Here the dependency versions of Spring Boot will be preferred to the ones of the SAP S/4HANA Cloud SDK. The only exceptions are two dependencies listed above, they fix version conflicts. Also, remember to replace any LATEST version.

Spring Initializr

In case you are using the Spring Initializr, do not forget adding “Web” as dependency. This ensures having the required dependency in your web application:

If you have used the Spring initializr, you were given a very basic project stub. It is a single Maven module with a “starter-parent” definition in the top. Although this is not the recommended software architecture, it is an easy and straight forward starting point. Now with the additions discussed above, the single POM file could look like this: 4.0.0 com.example demo 0.0.1-SNAPSHOT jar demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.7.RELEASE UTF-8 UTF-8 1.7 com.sap.cloud.s4hana sdk-bom LATEST pom import org.json json 20170516 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web com.sap.cloud.s4hana.cloudplatform scp-cf com.sap.cloud.s4hana s4hana-all com.sap.cloud.s4hana datamodel org.springframework.boot spring-boot-starter-test test com.sap.cloud.s4hana testutil test com.sap.cloud.s4hana.quality listeners-all test org.springframework.boot spring-boot-maven-plugin

Stay tuned for further updates and integration examples. http://bit.ly/2i7t1cP #SAP #SAPCloud #AI

Introducing the SAP Cloud Platform SDK for service development

At SAP TechEd Las Vegas this year Björn Goerke announced, among a lot of other great new things, the new SAP Cloud Platform SDK for service development. Generally speaking, it provides generic development libraries and tools such as OData services provisioning and consumption, business events and so on to enable application developers to easily create extensions on SAP Cloud Platform. In this blog I would like to provide you more insight about what the SDK does, how you can use it to build apps for SAP Cloud Platform and what resources help you to make best use of it.

Let’s first have a closer look at the scope of the SDK. As a “software development kit”, it helps you develop software, in this case apps which will run on SAP Cloud Platform. More specifically the SDK  at this point is a Java SDK and the resulting apps will run on SAP Cloud Platform, Cloud Foundry Environment.

Cloud apps are about providing and consuming services. This is where the SDK will help you by simplifying the development significantly. Apps built with the SDK can easily provision RESTful services based on open standard OData (V4) protocol, which is broadly adopted not only in the SAP world. The implementation for the different OData service operations (CRUDQ, actions, functions) can be done with very few lines of code, if it is not already done generically by the service provisioning framework of the SDK. Under the hood unsurprisingly we are using the Apache Olingo library for OData, which was largely contributed by SAP. The following code demonstrates some key attributes of the APIs of the SDK:

For the consumption of services again there are already powerful APIs for the most important datasources: OData (for instance for working with data in SAP S/4HANA) and CDS, which is the metamodel of choice now for business objects in SAP. The following self-explaning line of code shows an sample OData service request for retrieving product data from an S/4HANA system:

As you can easily imagine, as a Java developer you already now have some key ingredients for building what we call Extension Apps: these are apps, which provision services in the cloud (nothing special so far) but at the same time let you re-use all the data and processes, which you have already in place in your organization. Why bother building yet another store for products or business partners if you can simply use the existing one?

Beyond support for service-related development the SDK also offers you a growing number of capabilities which you will need for writing reliable, secure and manageable cloud apps.

So, when and how can you start using the SAP Cloud Platform SDK for service development?

You can start now, the SDK is publicly available on Maven Central under the GroupID com.sap.cloud.servicesdk and you can use it with your IDE of choice. It contains two archetypes: Quickstart (for your own project) and Sample (fully implemented app). We will continue to add functionality and datasources to the SDK and therefore you may want to come back from time to time to get the latest version.

In addition to the Javadocs that come with the SDK there is a detailed documentation on SAP Help Portal, which also contains currently four tutorials:

* Creating a Simple OData V4 Service That Exposes Mock Data
* Creating an OData V4 Service from an OData V2 Service
* Creating an OData V4 Service That Exposes a CDS Data Source
* Creating an OData V4 Service That Exposes Data From Multiple Data Sources

Also Arun Bhaskaran Nair from the development team has published a blog showing step by step how you in less than an hour can build an app with an OData service on SAP Cloud Platform by using the SDK.

Questions or feedback? Just Ask a Question, use the primary tag “development tools for SAP Cloud Platform” and the user tag “SAP Cloud Platform SDK for service development” http://bit.ly/2zxOYMq #SAP #SAPCloud #AI