Spring boot web applications with its default configurations are pretty easy to deploy in embedded tomcat server. We just need to run main method and it will start the web application. But it is not always the case as we might be developing the applications which are supposed to deploy in other servers like JBoss, Weblogic etc. In those cases we have to make changes in configurations and add additional files to make it running in those servers. In this post I’ll explain the steps to make a web application deployable in JBoss EAPP/Wildfly.
(Here I assume that you already have a spring-boot web application which will run perfectly in an embedded tomcat server)
Step 1: Update pom.xml file
- Change package type to “war”
<packaging>war</packaging>
- Update “spring-boot-starter-web” dependency to exclude embedded tomcat server
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
- Add servlet api dependency with provided scope. This is required to build the application only if you didn’t provide the server as runtime in your workspace.
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency>
- Remove “spring-boot-maven-plugin” plugin if already exist
Step 2: Update your application’s main class
- Add
SpringBootServletInitializer
to extend your main class. - Override configure method and update as follows
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(YourApplication.class); }
Step 3: Add jboss-web.xml
- Create “webapp/WEB-INF” directory inside “main” directory(if not already exist)
- Create jboss-web.xml file inside WEB-INF directory and add below code. Make sure to update the context value as required for your application.
<?xml version="1.0" encoding="UTF-8"?> <jboss-web> <context-root>myapp</context-root> </jboss-web>
Thats it! Your application is ready to deploy in JBoss EAP/Wildfly.
Some key points to note:
- Server configurations made in application.properties can be used only for embedded tomcat server. For other server deployments we have to use their own implementation. (E.g., jboss-web.xml used above to provide context path for the application)
Refer to the below link for a sample hello-world example of a web application which is deployable in JBoss EAP/WildFly server.
https://github.com/sastrija/examples/tree/master/spring-boot/jboss/hello-world-web