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 RESTful application using Spring Rest.

In this scenario you learned about Spring Boot and how it can be used to create RESTful APIs. 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 an index.html file but without any server-side logic. After that we created the REST services and now we could test the full web application. Finally, we deployed everything to OpenShift.

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 RESTful APIs

Step 1 of 3

Step 1

Get project set up

1. Adding Spring MVC and Tomcat 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.

In addition we are going to utilize the Spring MVC project which contains many of the abstractions we will use to build our APIs. pom.xml

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

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 previous trainings.

2. Test the application locally

Run the application by executing the following command (it should exit automatically as we do not have any web code yet):

mvn spring-boot:run

You should eventually see a log line like Started Application in 4.497 seconds (JVM running for 9.785). Open the application by clicking on the Local Web Browser tab or clicking here. Then stop the application by pressing Ctrl+C.

Congratulations

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

Terminal
Local Web Browser
OpenShift Console