Red Hat's Spring Boot application on OpenShift 4
Tags: openshift, springboot, redhat, java
Published: 2024-03-05 20:02:53

Do you know that Red Hat offers and support its own brand of Spring Boot libraries? Do you know that Red Hat also offers libraries that makes build and deploy on OpenShift easier? 

---
_In this quick article you will learn how to build and deploy an example application on OpenShift 4!_

---

## First, let's review the environment and versions:
- Openshift Container Platform 4.14
- Red Hat build of Spring Boot 2.7
- Java 11 

## Diclaimer
This article expects that you have some knowledge on Spring Boot and Openshift

## Use Red Hat libraries
### Fix your libraries
Starting with version 2.7 of Spring Boot, instead of shipping its own version of Spring Boot libraries, Red Hat strategy is to allow the use of the same official version of Spring Boot and only override the components that Red Hat offers with its own version. 
The most notable example is the Undertow web container, which is offered on version 2.2.26.SP2-redhat-00001 at the time of this writing. This is how you override the libraries:

```xml
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>	
		<!-- Exclude the Tomcat dependency -->
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
	</exclusions>
</dependency>

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

<dependency>
	<groupId>io.undertow</groupId>
	<artifactId>undertow-core</artifactId>
	<version>2.2.26.SP2-redhat-00001</version>
	<scope>compile</scope>
</dependency>
```
Visit [Red Hat Spring Boot Component details page](https://access.redhat.com/articles/3348731) to a list of components and versions that overrides the provide Spring Boot libraries. 

### Include the Dekorate dependency

The [dekorate.io](dekorate.io) is a new project that facilitates build and deploying your application on OpenShift. It uses annotations to decorate your application with OpenShift own resources. To use it, first add the dependency in the `pom.xml` of your project.

```xml
<dependency>
	<groupId>io.dekorate</groupId>
	<artifactId>openshift-annotations</artifactId>
	<version>2.11.5.redhat-00017</version>
</dependency>
```
### Annotate your application

To take advantage of the Dekorate library, annotate your application with `@OpenshiftApplication` annotation to configure how it will be deployed in OpenShift. In this example, the application will run 3 replicas and a Route object will be created.  

```java
@SpringBootApplication
@OpenshiftApplication(replicas = 3, expose = true) 
public class SpringBootExampleAppApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootExampleAppApplication.class, args);
	}

}
```

### Set the s2i builder image
In some clusters, the default s2i builder image is incorrectly running pods with Java 8 version instead of Java 11. To fix that, add the following property in application.properties Spring Boot file:
```properties
dekorate.s2i.builder-image=registry.access.redhat.com/ubi9/openjdk-11:latest
```

## Finally, deploy your application

Now, the final step is to deploy your application on Openshift. Just run the following command on the application root folder after properly login on OpenShift cluster with the `oc` client library:

```bash
./mvnw clean install -Ddekorate.deploy=true
```
If everything runs correctly, your application will be running on 3 pods and a be accessible from a route.

## What is next?

Did you like this article? Then, visit the [example application repository in Git Hub](https://github.com/angelogalvao/redhat-openshift-examples/tree/main/spring-boot-example-app) to get more details on how to build and deploy Red Hat own version of Spring Boot on Openshift 4.