Time to Complete
10 Min.
Learn how-to develop a simple Spring Boot sample application that can be deployed to SAP Cloud Platform
This document will teach you how-to develop a simple web application using Spring Boot. Given Spring Boot’s approach of embedding a servlet container (such as Tomcat, Jetty or Undertow) directly into an executable JAR
instead of creating a WAR
file that needs to be explicitly deployed to a runtime environment, this aspect will be the main focus of this how-to exercise. For the sake of simplicity, we keep the rest of the coding fairly basic in order to not distract you from the important stuff.
Create a new Maven Project
using the respective wizard.
Create a simple project (skip archetype selection)
option at the topworkspace location
of choiceDuring the course of this exercise we will use the following data:
Field | Value |
---|---|
Group Id | com.sap.hana.cloud.samples |
Artifact Id | cloud-spring-boot-sample |
Versioning | 0.0.1-SNAPSHOT |
Packaging | WAR |
Name | cloud-spring-boot-sample |
Now comes the ‘magic’ part of it. First, we will we will update the pom.xml
file to add a reference to the Spring Boot parent
project, from which our project will inherit sensible defaults and configurations. Furthermore, we will add code to include so-called starters
, which basically define a set of dependencies.
pom.xml
fileparent
information snippet below and paste them just underneath the <name></name>
attribute <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<parent></parent>
section we just added<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
</properties>
starters
dependencies<dependencies>
<!-- developer tools for hot code-replacement etc. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--Embedded tomcat etc. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
Note: The first dependency to
spring-boot-devtools
is optional and just something that comes in quite handy to accelerate development via hot-code replacement etc. Please refer to Developer Tools for more details.Please also note that we explicitly specified the last two
dependencies
asprovided
, hereby specifying that thesedependencies
will not be included in the resultingWAR
file once a build is triggered. The reason we do this is that we don’t need an embedded Tomcat (see Tomcat 8), nor the logging frameworks as the target runtime of SAP Cloud Platform already provides them out-of-the-box; hence including these libraries would only lead to strange classcloading issues. Please refer to the online documentation for further details.
(Optional) If you intend to deploy the application to a Cloud Foundry environment, such as the SAP Cloud Platform, Starter Edition for Cloud Foundry Services (Beta), then we would need to include the logging frameworks. In order to facilitate this setup we have introduced Maven profiles
so that we can simply trigger a build
for the respective deployment platform.
Copy the profiles
information snippet below and paste them just underneath the <dependencies></dependecies>
section
<profiles>
<!-- local/development profile -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- CF profile -->
<profile>
<id>cf</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
> Note: You can trigger the build
to create a WAR
file that includes the spring-boot-starter-logging
dependencies as needed for Cloud Foundry by running the following command: mvn -P cf build package
.
> Note: The complete pom.xml
used in this how-to document can be obtained as a reference from the respective GitHub repository.
Create a main Application
class as needed to bootstrap a Spring Boot web application:
Application
in package com.sap.hana.cloud.samples.springboot
package com.sap.hana.cloud.samples.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer
{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
{
return application.sources(Application.class);
}
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
Note: Please refer to the official Spring Boot documentation for more details about the
SpringBootServletInitializer
interface implemented here as an alternative to the classicweb.xml
approach. See: Traditional deployment.
Next, we’ll create a simple Controller
class to render the classic “Hello World!”.
HomeController
in the package com.sap.hana.cloud.samples.springboot.web
package com.sap.hana.cloud.samples.springboot.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController
{
@RequestMapping("/")
public String home()
{
return "Hello again!";
}
}
mvn spring-boot:run
In order to build a WAR
file to be deployed to SAP Cloud Platform run the following command:
mvn clean package
Note: Please refer to the official online documentation in case you need further instructions on how-to deploy an app to SAP Cloud Platform.
Updated 03/04/2018
Contributors Provide Feedback
10 Min.