Chapter 4. Developing an application for the Spring Boot runtime
The recommended approach for specifying and using supported and tested Maven artifacts in a Spring Boot application is to use the OpenShift Application Runtimes Spring Boot BOM.
4.1. Creating a basic Spring Boot application
In addition to using a booster, you can create new Spring Boot applications from scratch and deploy them to OpenShift.
4.1.1. Creating an application
Create a simple Greeting application to run on OpenShift using Spring Boot. The following procedure shows you how to:
- Write some simple application code that makes use of functionalities provided by Spring Boot.
-
Declare dependencies and configure the application build using a
pom.xmlfile. - Start your application on localhost and verify that it works.
Prerequisites
- Maven installed.
- JDK 8 or later installed.
Procedure
Create the application directory and navigate to it.
$ mkdir myApp $ cd myApp
Create a
pom.xmlfile.<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-app</artifactId> <version>1.0.0-SNAPSHOT</version> <name>MyApp</name> <description>My Application</description> <properties> <fabric8.generator.from>registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift:1.2</fabric8.generator.from> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>me.snowdrop</groupId> <artifactId>spring-boot-bom</artifactId> <version>1.5.16.Final-redhat-00001</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxrs</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.16.RELEASE</version> </plugin> </plugins> </build> <!-- Specify the repositories containing RHOAR artifacts --> <repositories> <repository> <id>redhat-ga</id> <name>Red Hat GA Repository</name> <url>https://maven.repository.redhat.com/ga/</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>redhat-ga</id> <name>Red Hat GA Repository</name> <url>https://maven.repository.redhat.com/ga/</url> </pluginRepository> </pluginRepositories> <profiles> <profile> <id>openshift</id> <build> <plugins> <plugin> <groupId>io.fabric8</groupId> <artifactId>fabric8-maven-plugin</artifactId> <version>3.5.40</version> <executions> <execution> <goals> <goal>resource</goal> <goal>build</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>Create a new class in
src/main/java/com/example/.As a recommended practice, ensure that the location of your class within the directory structure of your project reflects the value that you set for
groupIdin yourpom.xmlfile. For example, for<groupId>my.awesome.project</groupId>, the location of the class should besrc/main/java/my/awesome/project/.package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } @RequestMapping("/") @ResponseBody public Message displayMessage() { return new Message(); } static class Message { private String content = "Greetings!"; public String getContent() { return content; } public void setContent(String content) { this.content = content; } } }Start your application. Execute the following command in the directory containing you application.
$ mvn spring-boot:run
Using
curlor your browser, verify your application is running athttp://localhost:8080.$ curl http://localhost:8080 {"content":"Greetings!"}
Additional information
- As a recommended practice, you can configure liveness and readiness probes to enable health monitoring for your application when running on OpenShift. To learn how application health monitoring on OpenShift works, try the Health Check booster.
4.1.2. Deploying an application to OpenShift
This procedure shows you how to:
- Build your application and deploy it to OpenShift using the Fabric8 Maven Plugin.
- Use the command line to interact with your application running on OpenShift.
Prerequisites
-
The
ocCLI client installed. - Maven installed.
- A Maven-based application.
Procedure
Log in to your OpenShift instance with the
occlient.$ oc login ...
Create a new project.
$ oc new-project MY_PROJECT_NAME
In a terminal application, navigate to the directory containing your application:
$ cd myApp
Use Maven to start the deployment to OpenShift.
$ mvn clean fabric8:deploy -Popenshift
This command uses the Fabric8 Maven Plugin to launch the S2I process on OpenShift and to start the pod.
Check the status of your booster and ensure your pod is running.
$ oc get pods -w NAME READY STATUS RESTARTS AGE MY_APP_NAME-1-aaaaa 1/1 Running 0 58s MY_APP_NAME-s2i-1-build 0/1 Completed 0 2m
The
MY_APP_NAME-1-aaaaapod should have a status ofRunningonce it is fully deployed and started. Your specific pod name will vary. The number in the middle will increase with each new build. The letters at the end are generated when the pod is created.Once your booster is deployed and started, determine its route.
Example Route Information
$ oc get routes NAME HOST/PORT PATH SERVICES PORT TERMINATION MY_APP_NAME MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME MY_APP_NAME 8080
The route information of a pod gives you the base URL which you use to access it. In the example above, you would use
http://MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAMEas the base URL to access the application.Using
curlor your browser, verify your application is running in OpenShift.$ curl http://MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME {"content":"Greetings!"}
4.2. Deploying an existing Spring Boot application to OpenShift
You can easily deploy your existing application to OpenShift using the Fabric8 Maven plugin.
Prerequisites
- A Spring Boot–based application
Procedure
Add the following profile to the
pom.xmlfile in the root directory of your application:<profiles> <profile> <id>openshift</id> <build> <plugins> <plugin> <groupId>io.fabric8</groupId> <artifactId>fabric8-maven-plugin</artifactId> <version>3.5.40</version> <executions> <execution> <goals> <goal>resource</goal> <goal>build</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>In this profile, the Fabric8 Maven plugin is invoked for building and deploying the application to OpenShift.
- Deploy the application to OpenShift according to instructions in Section 4.1.2, “Deploying an application to OpenShift”.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.