In this scenario you will learn more about developing Spring Boot applications using the Red Hat 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.
More background and related information on Red Hat OpenShift Application Runtimes and Eclipse Vert.x can be found here:
- Red Hat 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.
Spring Boot RESTful APIs
Import the code
Let's refresh the code we'll be using. Run the following command to clone the sample project:
cd /root/projects && rm -rf rhoar-getting-started && git clone https://github.com/openshift-katacoda/rhoar-getting-started && cd rhoar-getting-started/spring/spring-rest-services
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.
In addition we are going to utilize the Spring MVC project which contains many of the abstractions we will use to build our APIs. Observe the following code block in the
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 the previous trainings.
2. Test the application locally
Run the application by executing the following command:
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.
You have now successfully executed the first step in this scenario.