How to deploy Spring Boot application in IBM Liberty and WAS 8.5

We have developed RESTful services using Spring Boot in our last tutorial. Now we will learn How to deploy Spring Boot application in IBM Liberty and WAS 8.5. Many of us are still using IBM Application Servers and it’s very important to understand how we can deploy our spring boot code there.

Why ?

Since Spring Boot’s main objective is to support Microservices and Cloud Native Applications,it includes embedded Tomcat\Jetty. IBM is also moving towards a very similar direction with their Liberty Profile Application Server and Bluemix. Now Liberty has Maven plugin to create, build and deploy Spring Boot Application to IBM Liberty Profile Application Server, which is either hosted locally or remotely using Bluemix. They have also provided similar support for Cloud Foundry (Could Native). So it has become very crucial to understand how to deploy Spring Boot application in IBM Liberty and WAS 8.5 (Websphere Application Server 8.5).

Moreover, many of our existing infrastructure is already using Websphere Application Server 8.5 / IBM Liberty profile. If we want to enable Spring Boot with our current infrastructure we need to also find a way to deploy the application there.

I am not discussing in detailed on WebLogic/JBOSS (Wildfly) here, the same mechanics would apply there as well.

How ?

There are mainly two ways to configure & deploy Spring Boot application in IBM Liberty Profile Application Server. Here are the approaches we shall experiment with.

Once our Spring Boot Application is working in IBM Liberty Profile, we will then deploy the application in IBM WebSphere Application Server 8.5.

P.S – The Java code would stay same across each deployment process, however the configuration and pom file would be different.

IBM Liberty Profile

spring-boot-starter-parent

As you have already created some Spring Boot Application by now, you can find that in each pom file has a parent artifact id as spring-boot-starter-parent. We will keep that as is and deploy our app.

Make sure you have the latest version (8.5.5.9, at the time to writing this blog) of IBM Liberty Profile, also its good to have the full JEE server. If you are not sure check the read-me file for the server version and run the following command to install the full JEE7 profile.

./installUtility install javaee-7.0

Changes to Maven Configuration File

Since we were using embedded Tomcat server we were using a jar file to run our app, now we need to create a war file to deploy that to IBM Liberty Server. So at first change packaging to war from jar

Add the Tomcat starter to exclusion list.

pom.xml

XHTML

1

2

3

4

5

6

<exclusions>

<exclusion>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

</exclusion>

</exclusions>

Since we need Servlet 3.1 we need to add the dependency here.

pom.xml

XHTML

1

2

3

4

5

6

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.1.0</version>

<scope>provided</scope>

</dependency>

In case you are using an old application server and want to use Servlet 3.0, use the following dependency instead.

Change the Code

Still now we had the main method as the starting point of our Spring Boot Application, however since we are using a WAR file we can’t leverage that. We need to change our main class to following. I also have a REST service to test the app.

Deploy To Liberty Profile

You can manually drop the war file to the dropins folder once liberty has started it will install the app.

In case you are using eclipse, just add the war to the server, the app will be automatically compiled. One quick tip here is, if you don’t create the spring boot app from STS by selecting war packaging option, you may not see the war file listed here.In that case you need to build using Maven and deploy the war manually.

In case you are using IntelliJ IDEA, you need to add a Liberty server profile and add Maven build then deploy artifact. Every time you start the server or redeploy the app it will automatically compile everything and then deploy the war file.

Once you access the URL of the deployed app, Spring Boot will initialize and load the related configurations.

net.wasdev.wlp.starters.springboot

IBM has recently started supported Spring Boot using net.wasdev.wlp.starters.springboot. Let me provide you the pom I will be using here.

If you look closely there are many differences of packages here and you can see IBM provided starters has been used here.

The Java code we changed previously will stay same, however IBM has provided support for defining Context root and web.xml. Also you can add any html file. You need to create a folder named webapp at the same level of java and resources and add following files there. Here my directory structure.

Also added an index.html file to make sure its working as expected. When you build your app using the above pom file, it will add the webapp folder in your war file.

I have created the pom specific for development perspective. You can find more details here (http://liberty-app-accelerator.wasdev.developer.ibm.com/).

Here is my IntelliJ configuration.

Once you run the server you will get following logs.

IBM WebSphere Application Server 8.5

In order to deploy Spring Boot Application to IBM WebSphere Application Server 8.5 sever you need to have the latest fix pack installed to get the most compatibility. At the time of writing this post the latest fix pack is 8.5.5.9, please make to download and install it.

I have also tested using JDK 6,7 & 8, all of them are working fine. You need to generate the war file using Maven and deploy that to server. Use the above pom files to create the war file.

When using spring-boot-starter-parent, you need to manually downgrade the Servlet version to 3.0 since WAS 8.5.5.9 supports only till Servlet 3.0 and not 3.1. You need to use the following dependency.

pom.xml

XHTML

1

2

3

4

5

6

<dependency>

<groupId>javax</groupId>

<artifactId>javaee-api</artifactId>

<version>7.0</version>

<scope>provided</scope>

</dependency>

I was able to make both spring-boot-starter-parent & net.wasdev.wlp.starters.springboot working in IBM WebSphere Application Server 8.5. If you notice we had defined start-class in the properties section, WAS will automatically use it to load the Servlet Initializer during server startup.

You will not be able to use Rational Application Developer with the build, you probably need eclipse or IntelliJ IDEA for your development.

Conclusion

You will probably be using embedded Tomcat during your development and use a Maven build to create the WAR specific for Liberty or WAS 8.5. There might be conflicts with older version of the jars when you use WAS 8.5, however Liberty supports most of the latest versions. We will see more compatibility between Spring Boot and Liberty, however may not be so much for WAS 8.5.

This post is the 4th part of Getting Started with Spring Boot Series. Find all the parts here.

I am deploying this example in WAS 8.5, here is the error I am getting.

Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)