此内容没有您所选择的语言版本。

Chapter 3. Developing and deploying a Spring Boot runtime application


You can create new Spring Boot applications from scratch and deploy them to OpenShift. 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.

3.1. Developing Spring Boot application

For a basic Spring Boot application, you need to create the following:

  • A Java class containing Spring Boot methods.
  • A pom.xml file containing information required by Maven to build the application.

The following procedure creates a simple Greeting application that returns "{"content":"Greetings!"}" as response.

Note

For building and deploying your applications to OpenShift, Spring Boot 2.4 only supports builder images based on OpenJDK 8 and OpenJDK 11. Oracle JDK and OpenJDK 9 builder images are not supported.

Prerequisites

  • OpenJDK 8 or OpenJDK 11 installed.
  • Maven installed.

Procedure

  1. Create a new directory myApp, and navigate to it.

    $ mkdir myApp
    $ cd myApp
    Copy to Clipboard Toggle word wrap

    This is the root directory for the application.

  2. Create directory structure src/main/java/com/example/ in the root directory, and navigate to it.

    $ mkdir -p src/main/java/com/example/
    $ cd src/main/java/com/example/
    Copy to Clipboard Toggle word wrap
  3. Create a Java class file MyApp.java containing the application code.

    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;
            }
        }
    }
    Copy to Clipboard Toggle word wrap
  4. Create a pom.xml file in the application root directory myApp with the following content:

    <?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>
    
      <!-- Import dependencies from the Spring Boot BOM. -->
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>dev.snowdrop</groupId>
            <artifactId>snowdrop-dependencies</artifactId>
            <version>2.4.9.Final-redhat-00001</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
          <dependency>
            <groupId>io.dekorate</groupId>
            <artifactId>openshift-spring-starter</artifactId>
          </dependency>
          <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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
        </dependencies>
      </dependencyManagement>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.4.9</version>
          </plugin>
        </plugins>
      </build>
    
      <!-- Specify the repositories containing Spring Boot 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>
    
    </project>
    Copy to Clipboard Toggle word wrap
  5. Build the application using Maven from the root directory of the application.

    $ mvn clean package -Popenshift -Ddekorate.deploy=true
    Copy to Clipboard Toggle word wrap
  6. Verify that the application is running.

    Using curl or your browser, verify your application is running at http://localhost:8080.

    $ curl http://localhost:8080
    {"content":"Greetings!"}
    Copy to Clipboard Toggle word wrap

3.2. Deploying Spring Boot application to OpenShift

To deploy your Spring Boot application to OpenShift, configure the pom.xml to use the Dekorate Maven dependency.

You can specify the Dekorate Maven dependency in the pom.xml file as follows:

<dependency>
    <groupId>io.dekorate</groupId>
    <artifactId>openshift-spring-starter</artifactId>
</dependency>
Copy to Clipboard Toggle word wrap

You can specify a Java image in the application.properties file as follows:

dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-8:1.3
Copy to Clipboard Toggle word wrap

The images are available in the Red Hat Ecosystem Catalog.

3.2.1. Supported Java images for Spring Boot

Spring Boot is certified and tested with various Java images that are available for different operating systems. For example, Java images are available for RHEL 7 and RHEL 8 with OpenJDK 8 or OpenJDK 11.

You require Docker or podman authentication to access the RHEL 8 images in the Red Hat Ecosystem Catalog.

The following table lists the OpenJDK images supported by Spring Boot for different architectures. These container images are available in the Red Hat Ecosystem Catalog. In the catalog, you can search and download the images listed in the table below. The image pages contain authentication procedures required to access the images.

Expand
JDK (OS)Architecture supportedRed Hat Ecosystem Catalog

OpenJDK8 (RHEL 7)

x86_64

redhat-openjdk-18/openjdk18-openshift

OpenJDK11 (RHEL 7)

x86_64

openjdk/openjdk-11-rhel7

OpenJDK8 (RHEL 8)

x86_64

ubi8/openjdk-8-runtime

OpenJDK11 (RHEL 8)

x86_64, IBM Z, and IBM Power Systems

ubi8/openjdk-11

Note

The use of a RHEL 8-based container on a RHEL 7 host, for example with OpenShift 3 or OpenShift 4, has limited support. For more information, see the Red Hat Enterprise Linux Container Compatibility Matrix.

In the following procedure, a profile with Dekorate Maven dependency is used for building and deploying the application to OpenShift.

Prerequisites

Procedure

  1. Add the following content to the pom.xml file in the application root directory:

    ...
    
    <profiles>
        <profile>
          <id>openshift</id>
          <build>
            <plugins>
               <dependency>
                 <groupId>io.dekorate</groupId>
                 <artifactId>openshift-spring-starter</artifactId>
               </dependency>
            </plugins>
          </build>
        </profile>
    </profiles>
    Copy to Clipboard Toggle word wrap
  2. Set the Java image in the application.properties file.

    • x86_64 architecture

      • RHEL 7 with OpenJDK 8

        dekorate.s2i.builder-image=registry.access.redhat.com/ubi7/openjdk-8:1.3
        Copy to Clipboard Toggle word wrap
      • RHEL 7 with OpenJDK 11

        dekorate.s2i.builder-image=registry.access.redhat.com/openjdk/openjdk-11-rhel7:latest
        Copy to Clipboard Toggle word wrap
      • RHEL 8 with OpenJDK 8

        dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-8:1.3
        Copy to Clipboard Toggle word wrap
      • RHEL 8 with OpenJDK 11

        dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-11:latest
        Copy to Clipboard Toggle word wrap
    • x86_64, IBM Z, and IBM Power System architectures

      • RHEL 8 with OpenJDK 11

        dekorate.s2i.builder-image=registry.access.redhat.com/ubi8/openjdk-11:latest
        Copy to Clipboard Toggle word wrap

To deploy your Spring Boot application to OpenShift, you must perform the following:

  • Log in to your OpenShift instance.
  • Deploy the application to the OpenShift instance.

Prerequisites

  • oc CLI client installed.
  • Maven installed.

Procedure

  1. Log in to your OpenShift instance with the oc client.

    $ oc login ...
    Copy to Clipboard Toggle word wrap
  2. Create a new project in the OpenShift instance.

    $ oc new-project MY_PROJECT_NAME
    Copy to Clipboard Toggle word wrap
  3. Deploy the application to OpenShift using Maven from the application’s root directory. The root directory of an application contains the pom.xml file.

    $ mvn clean package -Popenshift -Ddekorate.deploy=true
    Copy to Clipboard Toggle word wrap

    This command uses Dekorate to launch the S2I process on OpenShift and start the pod.

  4. Verify the deployment.

    1. Check the status of your application 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
      Copy to Clipboard Toggle word wrap

      The MY_APP_NAME-1-aaaaa pod should have a status of Running once it is fully deployed and started.

      Your specific pod name will vary.

    2. Determine the route for the pod.

      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
      Copy to Clipboard Toggle word wrap

      The route information of a pod gives you the base URL which you use to access it.

      In this example, http://MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME is the base URL to access the application.

    3. Verify that your application is running in OpenShift.

      $ curl http://MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME
      {"content":"Greetings!"}
      Copy to Clipboard Toggle word wrap

To deploy your Spring Boot application to stand-alone Red Hat Enterprise Linux, configure the pom.xml file in the application, package it using Maven and deploy using the java -jar command.

Prerequisites

  • RHEL 7 or RHEL 8 installed.

For deploying your Spring Boot application to stand-alone Red Hat Enterprise Linux, you must first package the application using Maven.

Prerequisites

  • Maven installed.

Procedure

  1. Add the following content to the pom.xml file in the application’s root directory:

      ...
      <!-- Specify target artifact type for the repackage goal. -->
      <packaging>jar</packaging>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <executions>
              <execution>
                  <goals>
                   <goal>repackage</goal>
                 </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      ...
    Copy to Clipboard Toggle word wrap
  2. Package your application using Maven.

    $ mvn clean package
    Copy to Clipboard Toggle word wrap

    The resulting JAR file is in the target directory.

To deploy your Spring Boot application to stand-alone Red Hat Enterprise Linux, use java -jar command.

Prerequisites

  • RHEL 7 or RHEL 8 installed.
  • OpenJDK 8 or OpenJDK 11 installed.
  • A JAR file with the application.

Procedure

  1. Deploy the JAR file with the application.

    $ java -jar my-project-1.0.0.jar
    Copy to Clipboard Toggle word wrap
  2. Verify the deployment.

    Use curl or your browser to verify your application is running at http://localhost:8080:

    $ curl http://localhost:8080
    Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat