Deploy Spring Boot on WildFly application server
JakMar17
Posted on March 17, 2021
By default Spring Boot comes with embedded Tomcat server, which, when build as fat jar enables deploying application as simple as running java -jar
command. But one could also use some other JavaEE or application server for deployment. Let's look how to deploy Spring Boot project to WildFly.
Disclaimer
For most of the time deploying Spring Boot on application server like WildFly does not actually makes sense since embedded servers (Tomcat by default, Jetty,...) are part of Spring Boot itself.
Deploying Spring Boot project to WildFly will be divided into three parts:
- Setting up WildFly (standalone edition)
- Setting up and configuring new Spring Boot project
- Building Spring Boot project and deploying
1. Setting up WildFly
Firstly download WildFly application server, after download is complete unzip archive, save it and open terminal in WildFly directory.
Next step is setting up WildFly user, with terminal move into bin
sub directory and run ./add-user.sh
on Linux or add-user.bat
on Windows. Then simply follow those steps:
> What type of user do you wish to add?
a (Management User)
> Enter the details of the new user to add.
Username: wildfly
Password: w1ldFly!
Re-enter Password : w1ldFly!
> What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[ ]: <LEAVE BLANK>
> Is this correct yes/no?
yes
> yes/no?
yes
> Press any key to continue . . .
With tha done, we can run WildFly server using ./standalone.sh
(Linux) or .\standalone.bat
(Windows). When initialization is completed you should see log:
15:28:47,740 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
Now enter http://127.0.0.1:9990
into the browser, after entering admin credentials (defined in previous step) you should see WildFly admin console:
With this we are done setting WildFly server and we are moving on to setting up Spring Boot project.
2. Setting up and configuring Spring Boot project
Firstly let's create project on Spring Boot website, here the only important thing is choosing War
as packaging. And also Maven if you want to follow next steps.
Then click Generate
button, download, unzip archive and import project into your favourite IDE (I will be using IntelliJ, but you could also use Eclipse, NetBeans, VSCode or anything else).
For testing purposes, we will be creating a simple HelloWorld API endpoint. Let's create Java file under /src/main/java//HelloApi.java
in it write following code (package and imports are not shown for readbility, but at the end of this article I posted Git repository with source code):
@RestController
public class HelloApi {
@GetMapping("/hello")
public String hello() {
return "Hello from Spring Boot";
}
}
You could try building (mvn clean package
) and running (java -jar /target/<APP-NAME>.jar
). While accessing http://localhost:8080/hello
you should see Hello from Spring Boot
.
Now let's configure Spring Boot project to deploy it on WebFly. Firstly we will remove Tomcat embedded server from out project. In the pom.xml
find spring-boot-starter-tomcat
dependency and make sure that <scope>
is set to provided
if not set it:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
And lets exclude some logging libraries that may break deployment:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
Next thing we will add some WildFly properties:
<properties>
<java.version>15</java.version>
<deploy.wildfly.host>127.0.0.1</deploy.wildfly.host>
<deploy.wildfly.port>9990</deploy.wildfly.port>
<deploy.wildfly.username>wildfly</deploy.wildfly.username>
<deploy.wildfly.password>w1ldFly!</deploy.wildfly.password>
</properties>
Let's break it down: java.version
defines JDK version that project is build upon (it is already set), deploy.wildfly.XX
are properties that we need for deploying app on WildFly, host
and port
are where WildFly is "living", username
and password
are same as defined in previous step.
Lets use them in build plugin:
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>2.0.2.Final</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
<configuration>
<filename>${project.build.finalName}.war</filename>
<hostname>${deploy.wildfly.host}</hostname>
<port>${deploy.wildfly.port}</port>
<username>${deploy.wildfly.username}</username>
<password>${deploy.wildfly.password}</password>
</configuration>
</plugin>
With that done make sure everything is still building (run mvn clean package
) and lets move on to the next step.
Last thing we will do in our Spring Boot project is setting up context root path (we will set it up to /
so our app will be available on domain.com/hello
). Firstly create new file under /src/main/webapp/WEB-INF/jboss-web.xml
secondly add to the file:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.jboss.com/xml/ns/javaee
http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
<context-root>/</context-root>
</jboss-web>
Here the only important part is <context-root>/</context-root>
where we define context-root.
With this out of the way we build out project (mvn clean package
) and deploy our Spring Boot project to WildFly server.
3. Deploying Spring Boot to WildFly
After succesful build you should find .war
file in /target
directory, this is our project's final form that we will deploy to WildFly.
In WildFly's admin console chose Deployments
in navbar and then Upload Deployment
.
Now all we have to do is uplaod war
file and deploy it.
After successful deployment you could go to http://127.0.0.1:8080/hello and see our final result.
We could (and should) skip last step altogether and simply use Maven for deploying to WildFly by using
mvn clean install
Source code is available on [GitHub](https://github.com/JakMar17/SpringBoot-WildFly)
*Cover photo by Min An from Pexels*
Posted on March 17, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.