Forem

KenjiGoh
KenjiGoh

Posted on

Basic Setup: Run SpringBoot using JBoss (Wildfly)

This tutorial assumes you have already set up Java and Maven in your system.

1. Install JBoss (Wildfly)

Go wildfly download page and download the tar file. For example, I downloaded version 35.0.1.Final.

Image description

Once downloaded, run this to unzip:

tar -xvzf wildfly-<version>.tar.gz
cd wildfly-<version>/

tar -xvzf wildfly-35.0.1.Final.tar.gz
cd wildfly-35.0.1.Final/

Enter fullscreen mode Exit fullscreen mode

2. Test start the Jboss server

cd wildfly-35.0.1.Final/
./bin/standalone.sh
Enter fullscreen mode Exit fullscreen mode

The default port is 8080, you can change port like this:

./bin/standalone.sh -Djboss.http.port=9090
Enter fullscreen mode Exit fullscreen mode

Open http://localhost:9090 to see this:

Image description

3. Download and package a springboot app

Go start spring and select maven.
By default, maven will build jar. We need to use war for JBoss server.
Locate your pom.xml and update your build to war:

<packaging>war</packaging>
Enter fullscreen mode Exit fullscreen mode

Exclude the embedded tomcat:

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

Add spring-boot-starter-tomcat as provided:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Modify the SpringBootApplication Class to extend from SpringBootServletInitializer:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Do some minimal changes to your springboot app for easier validation:
Add ThymeLeaf in pom.xl

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
Enter fullscreen mode Exit fullscreen mode

Add a HTML under resources\templates:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Home Page</title>
</head>
<body>
    <h1 th:text="${message}">Welcome to the Home Page</h1>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Add a HomeController

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HomeController {

    @GetMapping("/home")
    public String home(@RequestParam(name = "message", required = false, defaultValue = "Hello, Thymeleaf!") String message, Model model) {
        model.addAttribute("message", message);
        return "home";
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Start the war package

Build the WAR

mvn clean package
Enter fullscreen mode Exit fullscreen mode

Copy the WAR file to JBoss deployments folder

cp target/demo-0.0.1-SNAPSHOT.war $JBOSS_HOME/standalone/deployments/
Enter fullscreen mode Exit fullscreen mode

Image description

5. Finally start your demo springboot app with Jboss server!

http://localhost:9090/demo-0.0.1-SNAPSHOT/home?message=Springboot%20is%20running%20on%20jboss%20now!
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)