Difficulty: beginner
Estimated Time: 15 minutes

In this scenario, you will learn more about developer application using Spring Boot using Red Hat OpenShift Application Runtimes.

What you will learn

You will learn how to get started with building a CRUD (Create, Read, Update and Delete) web application.

How is Spring Boot supported on OpenShift?

Spring is one of the most popular Java Frameworks and offers an alternative to the Java EE programming model. Spring is also very popular for build application based on Microservices Architecture. Spring Boot is a popular tool in the Spring eco system that helps with organizing/using 3rd party libraries together with Spring and also provides a mechanism for boot strapping embeddable runtimes, like Apache Tomcat. Bootable applications (sometimes also called fat jars) fits the container model very well since in a container platform like OpenShift responsibilities like starting, stopping and monitoring applications are then handled by the container platform instead of an Application Server.

Red Hat test and certify a certain version of Spring and Spring Boot for usage on the OpenShift platform as part of Red Hat OpenShift Application Runtimes. Red Hat also provides full support for Apache Tomcat, Hibernate and Apache CXF (for REST services) when used in a Spring Boot application on OpenShift Application Runtimes.

In this scenario, you learned about Spring Boot, and how it can be used to create CRUD applications. We will add additional scenarios that explain more how to build Spring Boot applications on OpenShift shortly, so check back to learn.openshift.com

To summarize, you started by adding spring-boot-starter-web which brought in an supported version of embedded Apache Tomcat from the Red Hat maven repository (since we used the BOM from Snowdrop). This enabled us to have a running application returning index.html, but without any server-side logic. After that, we added spring-boot-data-jpa to bring in Hibernate and developed a Fruit Entity and a Repository for database access. Then you executed a set of test cases via spring-boot-starter-test to verify that our Fruit Repository worked as it should. After that, we created the REST services, and now we could test the full web application. Finally, we deployed everything to OpenShift after creating a database instance and adding the database driver.

Spring Boot is a powerful and easy to use framework for developing everything from microservices to monolith application, but Spring Boot becomes every better when we package it in a container and use an orchestrated platform like OpenShift. When using OpenShift, we can in a very consistent and fast way deploy our Spring application, in an exact copy of our production environment. OpenShift also enables things like Deployment pipeline and Blue/Green deployment, but that was not covered in this scenario.

Additional Resources

More background and related information on Red Hat OpenShift Application Runtimes and Eclipse Vert.x can be found here:

  • Red Hat OpenShift Application Runtimes for Developers - Here you can get started with a project using different boosters and clone that project to your local computer. This also enables you to deploy your application on your own private OpenShift Container Platform or use OpenShift Online that is provided for free from Red Hat.
  • Project Snowdrop homepage - This site has a lot of details of the work that Red Hat is doing to make Spring work great on Kubernetes and OpenShift.

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Spring Boot development on OpenShift

Step 1 of 5

Step 1

Review the base structure of the application

For your convenience, this scenario has been created with a base project using the Java programming language and the Apache Maven build tool.

Initially, the project is almost empty and doesn't do anything. Start by reviewing the content by executing a tree in your terminal.

The output should look something like this

.
|-- pom.xml
`-- src
    `-- main
        |-- fabric8
        |   |-- credentials-secret.yml
        |   |-- deployment.yml
        |   `-- route.yml
        |-- java
        |   `-- com
        |       `-- example
        |           |-- Application.java
        |           `-- booster
        |               `-- service
        `-- resources
            |-- application-local.properties
            |-- application-openshift.properties
            `-- static
                `-- index.html

As you can see, there are some files that we have prepared for you in the project. Under src/main/resources/index.html we have for example prepared an html file for you. Except for the fabric8 directory and the index.html, this matches very well what you would get if you generate an empty project from the Spring Initializr web page. For the moment you can ignore the content of the fabric8 folder (we will discuss this later).

One this that differs slightly is the pom.xml. Please open the and examen it a bit closer (but do not change anything at this time)

pom.xml

As you review the content, you will notice that there are a lot of TODO comments. Do not remove them! These comments are used as a marker and without them, you will not be able to finish this scenario.

Notice that we are not using the default BOM (Bill of material) that Spring Boot project typically use. Instead, we are using a BOM provided by Red Hat as part of the Snowdrop project.

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>me.snowdrop</groupId>
        <artifactId>spring-boot-bom</artifactId>
        <version>${spring-boot.bom.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

We use this bill of material to make sure that we are using the version of for example Apache Tomcat that Red Hat supports.

1. Adding web (Apache Tomcat) to the application

Since our applications (like most) will be a web application, we need to use a servlet container like Apache Tomcat or Undertow. Since Red Hat offers support for Apache Tomcat (e.g., security patches, bug fixes, etc.), we will use it.

NOTE: Undertow is another an open source project that is maintained by Red Hat and therefore Red Hat plans to add support for Undertow shortly.

To add Apache Tomcat to our project all we have to do is to add the following lines in pom.xml

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

2. Test the application locally

As we develop the application, we might want to test and verify our change at different stages. We can do that locally, by using the spring-boot maven plugin.

Run the application by executing the below command:

mvn spring-boot:run

NOTE: The Katacoda terminal window is like your local terminal. Everything that you run here you should be able to execute on your local computer as long as you have a Java SDK 1.8 and Maven. In later steps, we will also use the oc command line tool.

3. Verify the application

To begin with, click on the Local Web Browser tab in the console frame of this browser window, which will open another tab or window of your browser pointing to port 8080 on your client.

Local Web Browser Tab

or use this link.

You should now see an HTML page that looks like this:

Local Web Browser Tab

As you can probably guess by now the application we are building is a Fruit repository where we create, read, update and delete different kinds of fruits.

NOTE: None of the button works at this stage since we haven't implemented services for them yet, but we will shortly do that.

4. Stop the application

Before moving on, click in the terminal window and then press CTRL-C to stop the running application!

Congratulations

You have now successfully executed the first step in this scenario.

Now you've seen how to get started with Spring Boot development on Red Hat OpenShift Application Runtimes

In next step of this scenario, we will add the logic to be able to read a list of fruits from the database.

Terminal
Local Web Browser
OpenShift Console