Learn how to migrate a Spring Boot MVC application to run in Open Liberty instead of an embedded container.
You will learn how to use Maven to migrate a sample Spring Boot MVC application to run in Open Liberty. The starting point of this guide will be the finished application from Spring’s Serving Web Content with Spring MVC guide. Please first go through that guide if you are not familiar with Spring Boot or Spring MVC. Note that Java 8 is required to run this project.
Several steps are required to perform the migration. First, you will remove the embedded Tomcat container. Next, you will replace the Spring Boot Maven Plugin with the Liberty Maven Plugin. Then, you will modify the application to run from a traditional WAR deployment. Finally, you will package and deploy the application to Open Liberty. Note that this guide will not cover migration of test cases, and the starting project does not include test code or dependencies.
You might first want to run the initial Spring Boot application without modifications. Move to the start
directory to run the sample application in embedded Tomcat:
cd start mvn spring-boot:run
Notice that the console output will display that the application is running in Tomcat on port 8080. Now access the application at the following URL:
http://localhost:8080/greeting?name=SpringBootApplication
You will see from the page output that the GreetingController
handled the name
request parameter:
Hello, SpringBootApplication!
You will now migrate the application to run in Open Liberty instead of embedded Tomcat. Make these changes in the start
folder.
Start by setting the application packaging type to liberty-assembly. No packaging type is specified in the starting project, so you’ll need to add the following line near the top of the POM, after the version
parameter.
<packaging>liberty-assembly</packaging>
Next, add the following property:
<properties> ... <start-class>hello.Application</start-class> ... </properties>
The`start-class` property references the fully qualified Spring Boot startup class. For our example, this class is hello.Application
. This property indicates the main class to use for the runnable JAR.
The spring-boot-starter-thymeleaf
artifact transitively depends on the spring-boot-starter-web
dependency. Because spring-boot-starter-web
uses Tomcat as the default embedded server, you will need to exclude it in our configuration since we want to run our application in Open Liberty. Thus, add the following exclusion to the spring-boot-starter-thymeleaf
dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <!-- Add this --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
You can also remove the spring-boot-devtools
dependency.
Finally, replace the spring-boot-maven-plugin
configuration with the liberty-maven-plugin
configuration as shown below.
<plugin> <groupId>net.wasdev.wlp.maven.plugins</groupId> <artifactId>liberty-maven-plugin</artifactId> <version>2.3</version> <extensions>true</extensions> <!-- Specify configuration, executions for liberty-maven-plugin --> <configuration> <serverName>MVCServer</serverName> <assemblyArtifact> <groupId>io.openliberty</groupId> <artifactId>openliberty-runtime</artifactId> <version>RELEASE</version> <type>zip</type> </assemblyArtifact> <assemblyInstallDirectory>${project.build.directory}/liberty</assemblyInstallDirectory> <packageFile>${project.build.directory}/MVCServerPackage.jar</packageFile> <features> <acceptLicense>true</acceptLicense> </features> <include>runnable</include> <installAppPackages>all</installAppPackages> <appsDirectory>apps</appsDirectory> <stripVersion>true</stripVersion> <looseApplication>true</looseApplication> </configuration> </plugin>
In this plugin, you specify an Open Liberty server called "MVCServer" which the application will be deployed to. You also set <include>runnable</include>
to indicate that the application will be packaged into a runnable JAR, which is set to be called MVCServerPackage.jar
in the <packageFile>
parameter.
Further documentation of the plugin configuration is provided in the ci.maven repository.
Next, you will provide the configuration for the Open Liberty server. Create a file called server.xml
and place it in src/test/resources
, which is the default directory for the configFile
configuration parameter in the liberty-maven-plugin
.
Add the following code to server.xml
:
<?xml version="1.0" encoding="UTF-8"?> <server description="new server"> <application context-root="/" location="gs-serving-web-content.war"></application> <!-- Enable features --> <featureManager> <feature>servlet-3.1</feature> </featureManager> <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" --> <httpEndpoint id="defaultHttpEndpoint" httpPort="9080" httpsPort="9443" /> <!-- Automatically expand WAR files and EAR files --> <applicationManager autoExpand="true" /> <!-- Automatically load the Spring application endpoint once the server is ready. --> <webContainer deferServletLoad="false"/> </server>
In the server configuration, the application context root is mapped to the server root directory. The servlet-3.1
feature is also needed for running our application, as well as other items which are described in the XML comments.
You will now need to change the startup process for your application. Traditionally, a Spring Boot application running on an embedded server such as Tomcat simply calls SpringApplication.run(…)
in its main class (in this case hello.Application
). However, your class needs to extend SpringBootServletInitializer
instead when being deployed to an Open Liberty server. Replace the original class with the following code:
package hello; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } }
Also recall that you previously set the <start-class>
parameter in the POM properties above to hello.Application
. This tells the server that your application starts its execution from this class.
This concludes the code changes; you are now ready to build and run your application in Open Liberty.
Now verify that your Spring Boot application is running in Open Liberty. Navigate to the following URL (notice that the port has changed since we define port 9080 in our Liberty configuration):
http://localhost:9080/greeting?name=SpringBootApplication
You will notice the same output as in the starting application.
You migrated a basic Spring Boot MVC application to run in Open Liberty.