DEV Community

loading...
Cover image for Deploy Spring Boot on WildFly application server

Deploy Spring Boot on WildFly application server

jakmar17 profile image JakMar17 Updated on ・5 min read

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:

  1. Setting up WildFly (standalone edition)
  2. Setting up and configuring new Spring Boot project
  3. 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 . . .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

Alt Text

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

Alt Text

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";
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

Alt Text

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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.

Alt Text

Now all we have to do is uplaod war file and deploy it.

Alt Text

After successful deployment you could go to http://127.0.0.1:8080/hello and see our final result.

Alt Text

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

Cover photo by Min An from Pexels

Discussion (6)

pic
Editor guide
Collapse
jeanp0 profile image
Jeanpier Mendoza

Careful, is jboss-web.xml, no jboss-xeb.xml

Collapse
jakmar17 profile image
JakMar17 Author

Yes! Thank you, fixed

Collapse
jeanp0 profile image
Jeanpier Mendoza

Thanks to you!

Collapse
chains5000 profile image
Pablo Fradua

We do run Spring Boot on Wildfly 16, our client's requirement, and haven't had any problem with the logback libraries (haven't excluded them). Our deploy process is different though.

Collapse
jakmar17 profile image
JakMar17 Author

Interesting, could you describe how your deploy process is different?

Collapse
chains5000 profile image
Pablo Fradua

Widlfy can autodeploy your WAR files, when copied to /standalone/deployments, and this location can be changed to anywhere you want to.
So, in our build process, Jenkins builds the war, copies it to the autodeploy folder, and then the application is reloaded.
Be aware, this is NOT the recommended approach, this is clearly stated in the Wildfly documentation, and should only be used for development.
We get occasional out of memory errors which require restarting the server, but this way of deployment is the client's requirement, so there's nothing we can do about it.