Difficulty: beginner
Estimated Time: 15 minutes

In this scenario you will learn more about developing Spring Boot applications using the Red Hat OpenShift Application Runtimes platform. We will be building a simple Spring Boot Web Application using the Model/View/Controller pattern with Spring's MVC framework.

In this scenario you learned about Spring Boot and how it can be used to create MVC-driven 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 an embedded Apache Tomcat Container from the Red Hat Maven Repository (since we used the BOM from Snowdrop). This enabled us to have a running web application returning a Thymeleaf template which built the Views necessary for our application. After that we created the Controller and now we could test the full web application. Finally, we deployed everything to OpenShift, made some changes, and re-deployed.

Spring Boot is a powerful and easy to use framework for developing everything from monolithic applications to microservices, 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 to multiple environments without worry of environment differences. OpenShift also enables things like Deployment Pipelines and Blue/Green deployments 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 MVC on RHOAR

Step 1 of 6

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.

1. Adding Spring MVC to the application

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

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.

Spring Boot has a pre-packaged POM (called a Starter POM) for the Spring MVC dependencies. This POM bundles everything you need to work with Spring MVC in a Boot application including an embedded Tomcat container. To add Spring MVC to our project, add the following lines in the pom.xml (If you left the TODO comments in you can click the Copy to Editor button to do this automatically)

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
      <version>1.5.8.RELEASE</version>
    </dependency>

NOTE: We include a specific version number for Thymeleaf as currently Snowdrop does not manage a version for this POM

There are other dependencies brought in by this Spring Starter POM. We will revisit these later in the scenario.

Note that the Thymeleaf dependency is our View technology which will be explained in the next section. For now just note that the spring-boot-starter-thymeleaf Starter POM is similar to the spring-boot-starter-web POM in that it brings in all the necessary dependencies for working with Thymeleaf in Spring Boot.

You may notice that there is no <version> tag here. That's because the version numbers are managed and automatically derived by the BOM mentioned in other modules.

2. Test and Verify the application locally

Run the application by executing the following command:

mvn spring-boot:run

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 or use this link.

At the moment this link should return a HTTP 404. But more importantly it returns what's called a Spring White-label page. In the absence of custom error pages Spring will return a White-label page with some error information. In some cases this can be a stack trace or an explicit error message. Because of this it's typically recommended to provide your own error pages. We will do this in the next step.

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

Congratulations

Now you've seen how to get started with Spring Boot development on Red Hat OpenShift Application Runtimes. In the next step you will create the Model and Controller portions of the web application.

Terminal
Local Web Browser
OpenShift Console