Running Spring Boot Application on SAP Cloud Platform

Cloud Foundry and Neo Environment

SAP Cloud Platform supports running J2EE applications. But most develops are not building Java Web Application from EJB/Servlet any more. They rather use framework to build up their app. Spring Framework from Pivotal is a popular one which you can build rapidly with just a few lines of codes and it is open source.

Getting a Spring Boot app running on SAP Cloud Platform is not so strait-forward as the platform already provides Tomcat container and some other stuffs which interfere with Spring Boot libraries so you need to work a bit more to get them running properly on the platform.

The introduction of SCP on Cloud Foundry makes also it easier to run. So in this guide, I will show you how to build a Hello World Spring Boot app on Cloud Foundry platform then will show what you need to do more to get it run on SAP-own Neo environment.

Create a Hello World Application

Create a new Maven project.

A New Project Named ‘spring-boot-helloscp’

Populate the pom.xml file with the following:

Create a package and create your application class:

Create a (REST)controller class and define your endpoint:

Build and test running your application and you should see a hello message at http://localhost:8080/

Build and Deploy on SCP Cloud Foundry

To deploy Spring Boot App on Cloud foundry environment, you need to exclude spring-boot-starter-tomcat from the JAR file by include the following in the pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

The tag <scope>provided</scope> means this library is already provided on the platform so no need to include it in the JAR.

Rebuild your project and upload deploy JAR file on SCP CF.

Deploying Application on SCP Cloud Foundry Environment

Note: If you don’t want to input application properties every time, you can create manifest.yml and select Use Manifest when deploying. See a sample of manifest file from here.

Wait until the application is started and turned green.

Application Dashboard on Cloud Foundry Environment

Click the URL to access your application.

Spring Boot Application on Cloud Foundry Environment

Deploy using Command Line Interface (CLI)

Deploying JAR file via the web interface most of the time fails with unknown error. There’s no log file telling you the root cause either.

Deploying JAR file Result in Unknown Error

So let’s use CF CLI to push codes to Clous Foundty and let it build and deploy for us.

Before we use the command, make sure to create manifest.yml first.

Log in to Cloud Foundry environment.

$ cf api https://api.cf.eu10.hana.ondemand.com
Setting api endpoint to https://api.cf.eu10.hana.ondemand.com...
OK
api endpoint:   https://api.cf.eu10.hana.ondemand.com
api version: 2.94.0
Not logged in. Use 'cf login' to log in.

Then

$ cf login
API endpoint: https://api.cf.eu10.hana.ondemand.com
Email> xxxxx@xxxxx.com
Password>
Authenticating...
OK
Targeted org Xxxxxxxxxxxtrial_trial
Targeted space dev
API endpoint:   https://api.cf.eu10.hana.ondemand.com (API version: 2.94.0)
User: xxxxx@xxxxx.com
Org:
Xxxxxxxxxxxtrial_trial
Space:
dev

After log in, then you can use command cf push. Then you should see something like this:

Showing health and status for app app12345 in org Xxxxxxxxxxxtrial_trial / space dev as xxxxx@xxxxx.com...
OK
requested state: started
instances: 1/1
usage: 512M x 1 instances
urls: app12345.cfapps.eu10.hana.ondemand.com
last uploaded: Sun Oct 1 04:24:50 UTC 2017
stack: cflinuxfs2
buildpack: client-certificate-mapper=1.3.0_RELEASE container-security-provider=1.10.0_RELEASE java-buildpack=v3.19-https://github.com/cloudfoundry/java-buildpack.git#727297d java-main open-jdk-like-jre=1.8.0_144 open-jdk-like-memory-calculator=2.0.2_RELEASE open-...
state     since                    cpu     memory           disk           details
#0
running 2017-10-01 11:26:01 AM 49.5% 282.1M of 512M 142M of 512M

This means your application is deployed and started.

Deployed Application via CF CLI

Refer this page for instructions to install CF CLI.

Build and Deploy on Neo Environment

Include one more provided dependency in pom.xml:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<scope>provided</scope>
</dependency>

Make the build to WAR by also adding this tag. Neo does not support JAR!

<packaging>war</packaging>

Make sure your application is extending SpringBootServletInitializer and override method configure. This will tells the platform which endpoint to bind and expose. Otherwise, you cannot access your application and always get 404 Not Found.

Build your application and deploy WAR file to SCP Neo.

Deploying Application on SCP Neo Environment

Start your application and click URL to access your application.

Application Dashboard on Neo Environment
Spring Boot Application on Neo Environment

Note: If you want to access your application at root URI then change finalName to ROOT in pom.xml (Any better way?!?)

<finalName>ROOT</finalName>

Deploying Options

Apart from web cockpit, you can deploy your application from Eclipse IDE or Console Client. For more information, check out this documentation:

Source Codes

My source codes are available at GitHub:

Make JAR, not WAR.