The following guide provides instructions for creating Jakarta EE applications from scratch, deploying them to WildFly running on different platforms and and testing the deployed applications.

The first four chapters guide you through creating, deploying and testing a Hello World Servlet application. The subsequent chapters guide you through creating applications based on other Jakarta EE technologies.

1. Creating a Maven project for a Hello World application

A Maven project contains a pom.xml configuration file and has the directory structure required for creating an application. You can configure the pom.xml configuration file to add dependencies for your application.

To create a Maven project for a Hello World application, follow these procedures:

1.1. Creating a Maven project with maven-archetype-webapp

Use the maven-archetype-webapp archetype to create a Maven project for building applications for WildFly deployment. Maven provides different archetypes for creating projects based on templates specific to project types. The maven-archetype-webapp creates a project with the structure required to develop simple web-applications.

Prerequisites
Procedure
  1. Set up a Maven project by using the mvn command. The command creates the directory structure for the project and the pom.xml configuration file.

    If you are not creating your first application, instead you are creating an application based on other examples in the guide, use the appropriate value for artifactId in the mvn command below. The correct value is mentioned in the "Prerequisites" section of each application-specific procedure. Alternatively, you can refer to artifact id to use for applications.

    $ mvn archetype:generate                          \
    -DgroupId=org.jboss.as.quickstarts                \(1)
    -DartifactId=helloworld                           \(2)
    -DarchetypeGroupId=org.apache.maven.archetypes    \(3)
    -DarchetypeArtifactId=maven-archetype-webapp      \(4)
    -DinteractiveMode=false                            (5)
    1 groupID uniquely identifies the project.
    2 artifactID is the name for the generated jar archive.
    3 The groupID of maven-archetype-webapp.
    4 The artifactID of maven-archetype-webapp.
    5 Tell Maven to use the supplied parameters rather than starting interactive mode.
  2. Navigate to the generated directory.

  3. Open the generated pom.xml configuration file in a text editor.

  4. Remove the content inside the <project> section of pom.xml configuration file after the <name>helloworld Maven Webapp</name> line.

    Ensure that the file looks like this:

    Example
    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.jboss.as.quickstarts</groupId>
        <artifactId>helloworld</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>
        <name>helloworld Maven Webapp</name>
      
    </project>

    The content was removed because it is not required for the application.

    Do not change the original artifactId value in the generated file. This value comes from the mvn archetype:generate command.

1.2. Defining properties in a Maven project

You can define properties in a Maven pom.xml configuration file as place holders for values. Define the value for WildFly server as a property to use the value consistently in the configuration.

Prerequisites
Procedure
  • Define a property <version.server> as the WildFly version on which you will deploy the configured application.

    <project>
       ...
        <properties>
            <!-- the Maven project should use the minimum Java SE version supported -->
            <maven.compiler.release>17</maven.compiler.release>
            <!-- the version for the Server -->
            <version.server>39.0.0.Final</version.server>
        </properties>
    </project>

1.3. Defining the repositories in a Maven project

Define the artifact and plug-in repositories in which Maven looks for artifacts and plug-ins to download.

Prerequisites
Procedure
  1. Define the artifacts repository.

    <project>
        ...
        <repositories>
            <repository>                                                                (1)
                <id>jboss-public-maven-repository</id>
                <name>JBoss Public Maven Repository</name>
                <url>https://repository.jboss.org/nexus/content/groups/public/</url>
                <releases>
                    <enabled>true</enabled>
                    <updatePolicy>never</updatePolicy>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>never</updatePolicy>
                </snapshots>
                <layout>default</layout>
            </repository>
        </repositories>
    </project>
    1 The JBoss Public Maven Repository provides artifacts such as WildFly Maven plug-ins.
  2. Define the plug-ins repository.

    <project>
        ...
        <pluginRepositories>
            <pluginRepository>
                <id>jboss-public-maven-repository</id>
                <name>JBoss Public Maven Repository</name>
                <url>https://repository.jboss.org/nexus/content/groups/public/</url>
                <releases>
                   <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </project>

1.4. Importing the WildFly BOMs as dependency management in a Maven project

Import the WildFly EE With Tools Bill of materials (BOM) to control the versions of runtime Maven dependencies. When you specify a BOM in the <dependencyManagement> section, you do not need to individually specify the versions of the Maven dependencies defined in the provided scope.

Prerequisites
Procedure
  1. Add a property for the BOM version in the properties section of the pom.xml configuration file.

    <properties>
        ...
        <version.bom.ee>${version.server}</version.bom.ee>
    </properties>

    The value defined in the property <version.server> is used as the value for BOM version.

  2. Import the WildFly BOMs dependency management.

    <project>
        ...
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.wildfly.bom</groupId>                 (1)
                    <artifactId>wildfly-ee-with-tools</artifactId>         (2)
                    <version>${version.bom.ee}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>
    1 GroupID of the WildFly-provided BOM.
    2 ArtifactID of the WildFly-provided BOM that provides supported WildFly Jakarta EE APIs plus additional WildFly API JARs and client BOMs, and development tools such as Arquillian.

1.5. Adding plug-in management in a Maven project

Add Maven plug-in management section to the pom.xml configuration file to get plug-ins required for Maven CLI commands.

Prerequisites
Procedure
  1. Define the versions for wildfly-maven-plugin and maven-war-plugin, in the <properties> section.

    <properties>
        ...
        <version.plugin.wildfly>5.1.0.Final</version.plugin.wildfly>
    </properties>
  2. Add <pluginManagement> in <build> section inside the <project> section.

    <project>
        ...
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>                                  (1)
                        <groupId>org.wildfly.plugins</groupId>
                        <artifactId>wildfly-maven-plugin</artifactId>
                        <version>${version.plugin.wildfly}</version>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>
    1 You can use the wildfly-maven-plugin to deploy an application to WildFly using the wildfly:deploy command.

1.6. Verifying a maven project

Verify that the Maven project you configured builds.

Prerequisites
Procedure
  • Build the project by using the package command.

    $ mvn package

    You get an output similar to the following:

    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    ...
Next steps

2. Creating a Hello World servlet application

Create a servlet application that returns "Hello world!" when accessed.

In this procedure, <application_home> refers to the directory that contains the pom.xml configuration file for the application. Also, <application_home> is the same as the artifactID that you used when creating the Maven project.

Prerequisites
Procedure
  1. Add the required dependency to pom.xml configuration file after the <dependencyManagement> section.

    <project>
        ...
        <dependencies>
            <dependency>                                             (1)
                <groupId>jakarta.servlet</groupId>
                <artifactId>jakarta.servlet-api</artifactId>
                <scope>provided</scope>                              (2)
            </dependency>
        </dependencies>
    </project>
    1 jakarta.servlet-api dependency provides Jakarta Servlet API.
    2 Define the scope as provided so that the dependency is not included in the application. The reason for not including the dependency in the application is that this dependency is managed by the wildfly-ee-with-tools BOM and such dependencies are included with WildFly.

    The dependency is defined without a version because wildfly-ee-with-tools BOM was imported in the <dependencyManagement> section.

  2. Navigate to the <application_home> directory.

  3. Create a directory to store the Java files.

    $ mkdir -p src/main/java/org/jboss/as/quickstarts/helloworld/
  4. Navigate to the new directory.

    $ cd src/main/java/org/jboss/as/quickstarts/helloworld/
  5. Create the Servlet HelloWorldServlet.java that returns "Hello World!".

    package org.jboss.as.quickstarts.helloworld;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import jakarta.servlet.ServletException;
    import jakarta.servlet.annotation.WebServlet;
    import jakarta.servlet.http.HttpServlet;
    import jakarta.servlet.http.HttpServletRequest;
    import jakarta.servlet.http.HttpServletResponse;
    
    @WebServlet("/HelloWorld")                      (1)
    public class HelloWorldServlet extends HttpServlet {
    
        static String PAGE_HEADER = "<html><head><title>helloworld</title></head><body>";
    
        static String PAGE_FOOTER = "</body></html>";
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("text/html");
            PrintWriter writer = resp.getWriter();
            writer.println(PAGE_HEADER);
            writer.println("<h1> Hello World! </h1>");
            writer.println(PAGE_FOOTER);
            writer.close();
        }
    }
    1 The @WebServlet("/HelloWorld") annotation provides the following information to WildFly:
    • This class is a servlet.

    • Make the servlet available at the URL "<application_URL>/HelloWorld".

      For example, if WildFly is running on the localhost and is available at the default HTTP port, 8080, the URL is http://localhost:8080/helloworld/HelloWorld.

  6. Navigate to the <application_home>/src/main/webapp directory.

    You find the file "index.jsp" that Maven created. This file prints "Hello World!" when you access the application.

  7. Update the "index.jsp" file to redirect to the Hello World servlet by replacing its content with the following content:

    <html>
        <head>
            <meta http-equiv="Refresh" content="0; URL=HelloWorld">
        </head>
    </html>
  8. Navigate to the <application_home> directory.

  9. Compile and package the application as a web archive (WAR) with the following command:

    $ mvn package
    Example output
    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    ...

3. Building and deploying an application to the server

You can deploy your application on a WildFly server running on bare metal or on OpenShift.

To deploy your application on a standard WildFly distribution running on bare metal, follow this procedure:

To deploy your application on a provisioned WildFly running on bare metal, follow this procedure:

To deploy your application on a WildFly server running on OpenShift, follow these procedures:

3.1. Building and deploying an application to a bare metal installation

You can deploy an application to WildFly by using the WildFly deploy plug-in.

Prerequisites
Procedure
  1. Navigate to the application root directory.

    The application root directory contains the pom.xml configuration file.

  2. Add the following build configuration to the pom.xml configuration file in the <project> section to define the application archive filename.

    <build>
        ...
        <finalName>${project.artifactId}</finalName>        (1)
    </build>
    1 Set the name of the deployment to the project’s artifact ID.
  3. Build and deploy the application by using the WildFly deploy plug-in.

    $ mvn package wildfly:deploy
Verification

3.2. Building and deploying an application on a provisioned server

Instead of deploying your applications to an existing WildFly server distribution, you can provision a WildFly server to deploy and run an application by using the WildFly Maven plug-in. The following procedure demonstrates configuring and using the WildFly Maven plug-in.

There are two ways to package an application in the provisioned server:

  • Provision the server with the application deployed in the target/server directory. This is similar to the standard application deployment. In this procedure, the "provisioned-server" profile is used for this kind of packaging.

  • Alternatively, you can package the server with the application deployed as an executable Java Archive (jar). In this case you cannot easily change the application as it is within a jar file. However, you can start the server with the application deployed by simply using the java -jar command. In this procedure, the "bootable-jar" profile is used for this kind of packaging.

In this procedure, <application_home> refers to the directory that contains the pom.xml configuration file for the application. Also, <application_home> is the same as the artifactID that you used when creating the Maven project.

Prerequisites
Procedure
  1. Navigate to the <application_home> directory.

  2. Create a profiles "provisioned-server", and "bootable-jar" in the pom.xml configuration file. These profiles configure the WildFly Maven plug-in for provisioning of a server with the application deployed.

    <project>
        ...
        <profiles>
            ...
            <profile>
                <id>provisioned-server</id>                            (1)
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.wildfly.plugins</groupId>
                            <artifactId>wildfly-maven-plugin</artifactId>
                            <configuration>
                                <discover-provisioning-info>             (2)
                                    <version>${version.server}</version>
                                </discover-provisioning-info>
                            </configuration>
                            <executions>
                                <execution>
                                    <goals>
                                        <goal>package</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
            <profile>
                <id>bootable-jar</id>                                   (3)
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.wildfly.plugins</groupId>
                            <artifactId>wildfly-maven-plugin</artifactId>
                            <configuration>
                                <discover-provisioning-info>           (4)
                                    <version>${version.server}</version>
                                </discover-provisioning-info>
                                <bootable-jar>true</bootable-jar>
                            </configuration>
                            <executions>
                                <execution>
                                    <goals>
                                        <goal>package</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    </project>
    1 Provision the server with the application deployed in the target/server directory.
    2 Use WildFly Glow to discover the feature packs and layers required to run the application, and provisions a server containing those layers.
    3 Provision and package the server with the application deployed as an executable jar.
    4 Use WildFly Glow to discover the feature packs and layers required to run the application, and provisions a server containing those layers.
  3. Define the application archive filename in the <build> section of <project> if you have not already defined it.

    <build>
        ...
        <finalName>${project.artifactId}</finalName>        (1)
    </build>
    1 Set the name of the deployment to the project’s artifact ID.
  4. Provision the server with the application deployed.

    $ mvn clean package
  5. Start the server.

    • To start the WildFly provisioned server based on the "provisioned-server" profile, use the WildFly Maven plug-in start goal.

      $ mvn wildfly:start
    • To start the WildFly provisioned server based on the "bootable-jar" profile, use the WildFly Maven plug-in start goal.

      $ mvn wildfly:start-jar

      Alternatively, you can start the server without using Maven.

      $ java -jar target/helloworld-bootable.jar
Verification
  1. Navigate to the address http://localhost:8080/helloworld in a browser.

    You are redirected to http://localhost:8080/helloworld/HelloWorld and you get the following message:

    Hello World!

You can stop the running server with the mvn wildfly:shutdown command.

3.3. Building and deploying an application to OpenShift

You can use the source-to-image (S2I) workflow to deploy your applications to WildFly on OpenShift. The S2I workflow takes source code from a Git repository and injects it into a container that’s based on the language and framework you want to use. After the S2I workflow is completed, the src code is compiled, the application is packaged and is deployed to the WildFly server.

3.3.1. Preparing an application for deployment on OpenShift

OpenShift uses application hosted on a Git repository. To deploy your application on OpenShift, you must first push your application to a Git repository. After that, you can use WildFly helm chart to configure your application deployment.

Prerequisites
Procedure
  1. Move the application to your local Git repository, if it already is not in it.

    $ mv -r helloworld/ <your_git_repo>
  2. Create a profile "openshift" in the pom.xml configuration file.

    This profile configures the WildFly maven plug-in, with respect to deployment on OpenShift.

    <project>
        ...
        <profiles>
        ...
            <profile>
                <id>openshift</id>
                    <build>
                        <plugins>
                            <plugin>
                                <groupId>org.wildfly.plugins</groupId>
                                <artifactId>wildfly-maven-plugin</artifactId>     (1)
                                <configuration>
                                <discover-provisioning-info>                      (2)
                                    <version>${version.server}</version>
                                    <context>cloud</context>
                                </discover-provisioning-info>
                            </configuration>
                            <executions>
                                <execution>
                                    <goals>
                                        <goal>package</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                        <!-- do not attach sources to openshift deployments -->
                        <plugin>                                                 (3)
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-source-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>attach-sources</id>
                                    <phase>none</phase>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    </project>
    1 wildfly-maven-plugin is a WildFly plug-in for provisioning a WildFly instance, with the application deployed on OpenShift.
    2 Use WildFly Glow to discover the feature packs and layers required to run the application, and provisions a server containing those layers.
    3 Prevent building of Java Archive (jar) files to decrease the build time and save disk space.
  3. Verify that the applications compiles.

    $ mvn package -Popenshift
  4. Clean the project so that the project contains only the sources.

    $ mvn clean
  5. Push the changes to your repository.

3.3.2. Building and deploying an application to WildFly on OpenShift with Helm

Use the WildFly Helm chart to configure and deploy application to WildFly on OpenShift with Helm.

Prerequisites
Procedure
  1. Create a directory called charts in the application root directoy and navigate to it. Application root directory is the one that contains pom.xml configuration file.

    $ mkdir charts; cd charts
  2. Create a file helm.yaml with the following content:

    build:
      uri: https://github.com/<user>/<repository>.git     (1)
      ref: <branch_name>                                  (2)
      contextDir: helloworld                              (3)
    deploy:
      replicas: 1                                         (4)
    1 Specify the URL for your Git repository that contains the application to deploy on OpenShift.
    2 Specify the Git branch that contains your application.
    3 Specify the directory containing the application.
    4 Specify the number of pods to create.
  3. Configure the WildFly repository in Helm.

    • If you haven’t added the WildFly repository to Helm before, add it.

      $ helm repo add wildfly https://docs.wildfly.org/wildfly-charts/
    • If you already have added the WildFly repository to Helm, update it.

      $ helm repo update wildfly
  4. Deploy the application using helm.

    $ helm install helloworld -f helm.yaml wildfly/wildfly
Verification
  1. Get the URL of the route to the deployment.

    $ APPLICATION_URL=https://$(oc get route helloworld --template='{{ .spec.host }}') &&
    echo "" &&
    echo "Application URL: $APPLICATION_URL"

    The deployment can take a few minutes to complete.

  2. To access the application, navigate to the following address in a browser:

    http://<APPLICATION_URL>/helloworld

    Replacing <APPLICATION_URL> with its value that is printed in the output of the command.

    You are redirected to http://<APPLICATION_URL>/helloworld/HelloWorld and you get the following message:

    Hello World!

4. Testing an application deployed on WildFly

To ensure that the Hello World application deployed on WildFly is working, you can add integration tests.

To add tests for an application deployed on a standard WildFly server, or an application deployed on a provisioned server running on bare metal, follow these procedures:

To add tests for an application deployed on a WildFly server running on OpenShift, follow these procedures:

4.1. Adding the Maven dependencies and profile required for integration tests

To create integration tests for your applications, add the required Maven dependencies.

Prerequisites
Procedure
  1. Define the following properties in the pom.xml configuration file:

    <properties>
        ...
        <version.plugin.failsafe>3.2.2</version.plugin.failsafe>
    </properties>
  2. Add the dependency required for tests.

    <project>
        ...
        <dependencies>
            ...
            <dependency>
               <groupId>junit</groupId>
               <artifactId>junit</artifactId>
               <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
  3. Define a profile to add the plug-ins required for integration tests.

    <project>
        ...
        <profiles>
            ...
            <profile>
                <id>integration-testing</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-failsafe-plugin</artifactId> (1)
                            <version>${version.plugin.failsafe}</version>
                            <configuration>
                                <includes>
                                    <include>**/HelloWorldServletIT</include>       (2)
                                </includes>
                            </configuration>
                            <executions>
                                <execution>
                                   <goals>
                                       <goal>integration-test</goal>
                                       <goal>verify</goal>
                                   </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    </project>
    1 Maven plug-in for running integration tests.
    2 The name of the Java class that tests the application.

4.2. Creating a test class to test an application

Create an integration test that verifies that the application is deployed and running on WildFly on OpenShift, by checking that the HTTP GET of its web page returns 200 OK.

In this procedure, <application_home> refers to the directory that contains the pom.xml configuration file for the application.

Prerequisites
Procedure
  1. Navigate to the <application_home> directory.

  2. Create a directory to store the test class.

    $ mkdir -p src/test/java/org/jboss/as/quickstarts/helloworld
  3. Navigate to the new directory.

    $ cd src/test/java/org/jboss/as/quickstarts/helloworld
  4. Create a Java class HelloWorldServletIT.java that tests the deployment.

    package org.jboss.as.quickstarts.helloworld;
    
    import org.junit.Test;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.net.http.HttpClient;
    import java.net.http.HttpRequest;
    import java.net.http.HttpResponse;
    import java.time.Duration;
    import static org.junit.Assert.assertEquals;
    
    public class HelloWorldServletIT {
    
        private static final String DEFAULT_SERVER_HOST = "http://localhost:8080/helloworld";                (1)
    
        @Test
        public void testHTTPEndpointIsAvailable() throws IOException, InterruptedException, URISyntaxException {
            String serverHost = System.getProperty("server.host");
            if (serverHost == null) {
                serverHost = DEFAULT_SERVER_HOST;
            }
            final HttpRequest request = HttpRequest.newBuilder()
                    .uri(new URI(serverHost+"/HelloWorld"))
                    .GET()
                    .build();                                                                                 (2)
            final HttpClient client = HttpClient.newBuilder()
                    .followRedirects(HttpClient.Redirect.ALWAYS)
                    .connectTimeout(Duration.ofMinutes(1))
                    .build();                                                                                 (3)
            final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); (4)
            assertEquals(200, response.statusCode());                                                         (5)
        }
    }
    1 The URL at which the application is running. This value is used if sever.host is undefined.
    2 Create an HttpRequest instance for the application URI.
    3 Create an HttpClient to send requests to and receive response from the application.
    4 Get response from the application.
    5 Test that the response received from the application is "200" indicating that the application is rechable.
Next steps

4.3. Testing an application deployed on WildFly that is running on bare metal

Test the application deployed on WildFly that is running on bare metal.

Prerequisites
Procedure
  1. Navigate to the <application_home> directory.

  2. Run the integration test by using the verify command with the integration-testing profile.

    $ mvn verify -Pintegration-testing
    Example output
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO]
    [INFO]
    [INFO] --- maven-failsafe-plugin:3.2.2:verify (default) @ helloworld ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  9.982 s
    [INFO] Finished at: 2023-11-22T14:53:54+05:30
    [INFO] ------------------------------------------------------------------------

4.4. Testing an application deployed to WildFly on OpenShift

Test the application deployed to WildFly on OpenShift.

Prerequisites
Procedure
  1. Push the changes to your Git repository.

  2. Navigate to the <application_home> directory.

  3. Run the test by using the verify command, activating the integration-testing profile and specifying the URL to the application.

    $ mvn verify -Pintegration-testing -Dserver.host=https://$(oc get route helloworld --template='{{ .spec.host }}')
    Example output
    [INFO] Running org.jboss.as.quickstarts.helloworld.HelloWorldServletIT
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.345 s -- in org.jboss.as.quickstarts.helloworld.HelloWorldServletIT
    [INFO]
    [INFO] Results:
    [INFO]
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO]
    [INFO]
    [INFO] --- maven-failsafe-plugin:3.2.2:verify (default) @ helloworld ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  2.984 s
    [INFO] Finished at: 2023-11-30T15:51:22+05:30
    [INFO] ------------------------------------------------------------------------

5. Developing a hello world RESTful service application

In the following examples, the artifactID used is helloworld-rs.

5.1. Creating a Hello World REST service

Create a Jakarta RESTful Web Service that returns "Hello world!" when accessed.

In this procedure, <application_home> refers to the directory that contains the pom.xml configuration file for the application. Also, <application_home> is the same as the artifactID that you used when creating the Maven project.

Prerequisites
Procedure
  1. Add the required dependency to pom.xml configuration file after the <dependencyManagement> section.

    <project>
        ...
        <dependencies>
             <dependency>                                  (1)
                <groupId>jakarta.ws.rs</groupId>
                <artifactId>jakarta.ws.rs-api</artifactId>
                <scope>provided</scope>                    (2)
            </dependency>
        </dependencies>
    </project>
    1 jakarta.ws.rs dependency provides Jakarta RESTful Web Services API.
    2 Define the scope as provided so that the dependency is not included in the application. The reason for not including the dependency in the application is that this dependency is managed by the wildfly-ee-with-tools BOM and such dependencies are included with WildFly.

    The dependency is defined without a version because wildfly-ee-with-tools BOM was imported in the <dependencyManagement> section.

  2. Navigate to the <application_home> directory.

  3. Create a directory to store the Java files.

    $ mkdir -p src/main/java/org/jboss/as/quickstarts/helloworld/rest
  4. Navigate to the new directory.

    $ cd src/main/java/org/jboss/as/quickstarts/helloworld/rest
  5. Create an Java class JakartaRESTActivator.java that extends the jakarta.ws.rs.core.Application class.

    The reason for extending the jakarta.ws.rs.core.Application class is that to define the base URI for your service, you must define the path by using the @ApplicationPath annotation, which can only be applied to a subclass of the jakarta.ws.rs.core.Application class.

    package org.jboss.as.quickstarts.rshelloworld;
    
    import jakarta.ws.rs.ApplicationPath;
    import jakarta.ws.rs.core.Application;
    
    /**
     * JakartaRESTActivator is an arbitrary name, what is important is that jakarta.ws.rs.core.Application is extended
     * and the @ApplicationPath annotation is used with a "rest" path.  Without this the rest routes linked to
     * from index.html would not be found.
     */
    @ApplicationPath("rest")                                 (1)
    public class JakartaRESTActivator extends Application {
        // Left empty intentionally
    }
    1 Define the base URI for your REST resources. For example, if WildFly is running on the localhost and is available at the default HTTP port, 8080, the base URI is http://localhost:8080/<project_name>/rest/. In this example, the URI will be http://localhost:8080/helloworld-rs/rest/
  6. Create a Java class HelloWorld.java that returns "Hello World!".

    package org.jboss.as.quickstarts.rshelloworld;
    
    import jakarta.ws.rs.GET;
    import jakarta.ws.rs.Path;
    import jakarta.ws.rs.Produces;
    import jakarta.ws.rs.core.MediaType;
    
    @Path("/")                              (1)
    public class HelloWorld {
    
        @GET                                (2)
        @Path("/HelloWorld")                (3)
        @Produces(MediaType.TEXT_PLAIN)     (4)
        public String hello() {
            return "Hello World!";
        }
    }
    1 Declare the class to be available at the application’s root URL.
    2 Use the @Get annotation to designate the method for processing HTTP GET requests.
    3 Declare the method to be available at the URL "/HelloWorld" relative to the base URL. In the previous step, the base URI was configured as "rest". Therefore, if WildFly is running on the localhost and is available at the default HTTP port, 8080, the base URL for this resource is http://localhost:8080/<project_name>/rest/HelloWorld.
    4 specify the MIME media types of representations this method can produce.
  7. Navigate to the <application_home>/src/main/webapp directory.

    You find the file "index.jsp" that Maven created. This file prints "Hello World!" when you access the application.

  8. Update the "index.jsp" file to redirect to the application URL by replacing its content with the following content:

    <html>
        <head>
            <meta http-equiv="Refresh" content="0; URL=rest/HelloWorld">
        </head>
    </html>
  9. Rename the file to index.html.

  10. Navigate to the <application_home> directory.

  11. Compile and package the application as a web archive (WAR) with the following command:

    $ mvn package
    Example output
    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    ...

5.2. Building and deploying a RESTful application to the server

You can deploy your application on a WildFly server running on bare metal or on OpenShift.

To deploy your application on a WildFly server running on bare metal, follow this procedure:

To deploy your application on a provisioned WildFly server, follow this procedure:

To deploy your application on a WildFly server running on OpenShift, follow this procedures:

5.2.1. Building and deploying a RESTful application to a bare metal installation

You can deploy an application to WildFly by using the WildFly deploy plug-in.

Prerequisites
Procedure
  1. Navigate to the application root directory.

    The application root directory contains the pom.xml configuration file.

  2. Add the following build configuration to the pom.xml configuration file in the <project> section to define the application archive filename.

    <build>
        ...
        <finalName>${project.artifactId}</finalName>        (1)
    </build>
    1 Set the name of the deployment to the project’s artifact ID.
  3. Build and deploy the application by using the WildFly deploy plug-in.

    $ mvn package wildfly:deploy
Verification

5.2.2. Building and deploying a RESTful application on a provisioned server

Instead of deploying your applications to an existing WildFly server distribution, you can provision a WildFly server to deploy and run an application by using the WildFly Maven plug-in. The following procedure demonstrates configuring and using the WildFly Maven plug-in.

In this procedure, <application_home> refers to the directory that contains the pom.xml configuration file for the application. Also, <application_home> is the same as the artifactID that you used when creating the Maven project.

Prerequisites
Procedure
  1. Navigate to the <application_home> directory.

    <project>
        ...
        <profiles>
            ...
            <profile>
                <id>provisioned-server</id>                            (1)
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.wildfly.plugins</groupId>
                            <artifactId>wildfly-maven-plugin</artifactId>
                            <configuration>
                                <discover-provisioning-info>             (2)
                                    <version>${version.server}</version>
                                </discover-provisioning-info>
                            </configuration>
                            <executions>
                                <execution>
                                    <goals>
                                        <goal>package</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    </project>
    1 Provision the server with the application deployed in the target/server directory.
    2 Use WildFly Glow to discover the feature packs and layers required to run the application, and provisions a server containing those layers.
  2. Define the application archive filename in the <build> section of <project> if you have not already defined it.

    <build>
        ...
        <finalName>${project.artifactId}</finalName>        (1)
    </build>
    1 Set the name of the deployment to the project’s artifact ID.
  3. Provision the server with the application deployed.

    $ mvn clean package
  4. Start the server.

    • To start the WildFly provisioned server based on the "provisioned-server" profile, use the WildFly Maven plug-in start goal.

      $ mvn wildfly:start
Verification

5.2.3. Building and deploying a RESTful application to OpenShift

You can use the source-to-image (S2I) workflow to deploy your applications to WildFly on OpenShift. The S2I workflow takes source code from a Git repository and injects it into a container that’s based on the language and framework you want to use. After the S2I workflow is completed, the src code is compiled, the application is packaged and is deployed to the WildFly server.

Preparing a RESTFul application for deployment on cloudPlatform

OpenShift uses application hosted on a Git repository. To deploy your application on OpenShift, you must first push your application to a Git repository. After that, you can use WildFly helm chart to configure your application deployment.

Prerequisites
Procedure
  1. Move the application to your local Git repository, if it already is not in it.

    $ mv -r helloworld/ <your_git_repo>
  2. Create a profile "openshift" in the pom.xml configuration file.

    This profile configures the WildFly maven plug-in, with respect to deployment on OpenShift.

    <project>
        ...
        <profiles>
        ...
            <profile>
                <id>openshift</id>
                    <build>
                        <plugins>
                            <plugin>
                                <groupId>org.wildfly.plugins</groupId>
                                <artifactId>wildfly-maven-plugin</artifactId>     (1)
                                <configuration>
                                <discover-provisioning-info>                      (2)
                                    <version>${version.server}</version>
                                    <context>cloud</context>
                                </discover-provisioning-info>
                            </configuration>
                            <executions>
                                <execution>
                                    <goals>
                                        <goal>package</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                        <!-- do not attach sources to openshift deployments -->
                        <plugin>                                                 (3)
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-source-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>attach-sources</id>
                                    <phase>none</phase>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    </project>
    1 wildfly-maven-plugin is a WildFly plug-in for provisioning a WildFly instance, with the application deployed on OpenShift.
    2 Use WildFly Glow to discover the feature packs and layers required to run the application, and provisions a server containing those layers.
    3 Prevent building of Java Archive (jar) files to decrease the build time and save disk space.
  3. Verify that the applications compiles.

    $ mvn package -Popenshift
  4. Clean the project so that the project contains only the sources.

    $ mvn clean
  5. Push the changes to your repository.

Building and deploying a RESTful application to server on OpenShift with Helm

Use the WildFly Helm chart to configure and deploy application to WildFly on OpenShift with Helm.

Prerequisites
Procedure
  1. Create a directory called charts in the application root directoy and navigate to it. Application root directory is the one that contains pom.xml configuration file.

    $ mkdir charts; cd charts
  2. Create a file helm.yaml with the following content:

    build:
      uri: https://github.com/<user>/<repository>.git     (1)
      ref: <branch_name>                                  (2)
      contextDir: helloworld-rs                              (3)
    deploy:
      replicas: 1                                         (4)
    1 Specify the URL for your Git repository that contains the application to deploy on OpenShift.
    2 Specify the Git branch that contains your application.
    3 Specify the directory containing the application.
    4 Specify the number of pods to create.
  3. Configure the WildFly repository in Helm.

    • If you haven’t added the WildFly repository to Helm before, add it.

      $ helm repo add wildfly https://docs.wildfly.org/wildfly-charts/
    • If you already have added the WildFly repository to Helm, update it.

      $ helm repo update wildfly
  4. Deploy the application using helm.

    $ helm install helloworld-rs -f helm.yaml wildfly/wildfly
Verification
  1. Get the URL of the route to the deployment.

    $ APPLICATION_URL=https://$(oc get route helloworld-rs --template='{{ .spec.host }}') &&
    echo "" &&
    echo "Application URL: $APPLICATION_URL"

    The deployment can take a few minutes to complete.

  2. To access the application, navigate to the following address in a browser:

    http://<APPLICATION_URL>/helloworld-rs

    Replacing <APPLICATION_URL> with its value that is printed in the output of the command.

    You are redirected to http://<APPLICATION_URL>/helloworld-rs/rest/HelloWorld and you get the following message:

    Hello World!

5.3. Testing a RESTful application deployed on WildFly

To ensure that the RESTful application deployed on WildFly is working, you can add integration tests.

To add tests for an application deployed on a standard WildFly server, or an application deployed on a provisioned server running on bare metal, follow these procedures:

To add tests for an application deployed on a WildFly server running on OpenShift, follow these procedures:

5.3.1. Adding the Maven dependencies and profile required for integration tests

To create integration tests for your applications, add the required Maven dependencies.

Prerequisites
Procedure
  1. Define the following properties in the pom.xml configuration file:

    <properties>
        ...
        <version.plugin.failsafe>3.2.2</version.plugin.failsafe>
    </properties>
  2. Add the dependency required for tests.

    <project>
        ...
        <dependencies>
            ...
            <dependency>
               <groupId>junit</groupId>
               <artifactId>junit</artifactId>
               <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
  3. Define a profile to add the plug-ins required for integration tests.

    <project>
        ...
        <profiles>
            ...
            <profile>
                <id>integration-testing</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-failsafe-plugin</artifactId> (1)
                            <version>${version.plugin.failsafe}</version>
                            <configuration>
                                <includes>
                                    <include>**/RESTEndPointIT.java</include>       (2)
                                </includes>
                            </configuration>
                            <executions>
                                <execution>
                                   <goals>
                                       <goal>integration-test</goal>
                                       <goal>verify</goal>
                                   </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    </project>
    1 Maven plug-in for running integration tests.
    2 The name of the Java class that tests the application.

5.3.2. Creating a test class to test the hello world RESTful service

Create an integration test that verifies that the hello world RESTful service retuns the string "Hello World!".

In this procedure, <application_home> refers to the directory that contains the pom.xml configuration file for the application.

Prerequisites
Procedure
  1. Add the required dependencies.

     <dependencies>
     ...
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-client</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- resteasy client needs commons-logging yet the server uses this instead,
                to be fully compatible on apps we need to add this dependency whenever commons-logging is needed,
                but on testing clients like this we could use commons-logging instead -->
            <dependency>
                <groupId>org.jboss.logging</groupId>
                <artifactId>commons-logging-jboss-logging</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-json-p-provider</artifactId>
                <scope>test</scope>
            </dependency>
           <!-- Required by the resteasy-json-p-provider -->
            <dependency>
                <groupId>org.eclipse.parsson</groupId>
                <artifactId>parsson</artifactId>
                <scope>test</scope>
            </dependency>
     </dependencies>
  2. Create a directory to store the test class.

    $ mkdir -p <application_home>/src/test/java/org/jboss/as/quickstarts/helloworld/rest
  3. Navigate to the new directory.

    $ cd src/test/java/org/jboss/as/quickstarts/helloworld/rest
  4. Create a Java class RESTEndPointIT.java that tests the deployment.

    package org.wildfly.quickstarts.rshelloworld;
    
    import jakarta.ws.rs.client.Client;
    import jakarta.ws.rs.client.ClientBuilder;
    import jakarta.ws.rs.client.WebTarget;
    import jakarta.ws.rs.core.MediaType;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import static org.junit.Assert.assertEquals;
    
    public class RESTEndPointIT {
        private Client client;
        private static final String DEFAULT_SERVER_HOST = "http://localhost:8080/helloworld-rs";
    
        private static String getServerHost() {
        String serverHost = System.getenv("SERVER_HOST");
            if (serverHost == null) {
                serverHost = System.getProperty("server.host");
            }
            if (serverHost == null) {
                serverHost = DEFAULT_SERVER_HOST;
            }
            return serverHost;
        }
    
        @Before
        public void before() {
            client = ClientBuilder.newClient();
        }
    
        @After
        public void after() {
            client.close();
        }
    
        @Test
        public void testRestEndPoint() {
            WebTarget target = client.target(getServerHost())
                    .path("/rest/HelloWorld");
            String responseMessage = target.request(MediaType.TEXT_PLAIN).get(String.class);
            assertEquals("Hello World!", responseMessage);
        }
    }
Next steps

Depending your deployment, follow the appropriate link from the list below to test the application:

5.3.3. Testing a RESTful application deployed on WildFly that is running on bare metal

Test the application deployed on WildFly that is running on bare metal.

Prerequisites
Procedure
  1. Navigate to the <application_home> directory.

  2. Run the integration test by using the verify command with the integration-testing profile.

    $ mvn verify -Pintegration-testing
    Example output
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO]
    [INFO]
    [INFO] --- maven-failsafe-plugin:3.2.2:verify (default) @ helloworld ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  9.982 s
    [INFO] Finished at: 2023-11-22T14:53:54+05:30
    [INFO] ------------------------------------------------------------------------

5.3.4. Testing a RESTful application deployed to WildFly on OpenShift

Test the application deployed to WildFly on OpenShift.

Prerequisites
Procedure
  1. Push the changes to your Git repository.

  2. Navigate to the <application_home> directory.

  3. Run the test by using the verify command, activating the integration-testing profile and specifying the URL to the application.

    $ mvn verify -Pintegration-testing -Dserver.host=https://$(oc get route helloworld-rs --template='{{ .spec.host }}')
    Example output
    [INFO] Running org.jboss.as.quickstarts.helloworld.HelloWorldServletIT
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.345 s -- in org.jboss.as.quickstarts.helloworld.HelloWorldServletIT
    [INFO]
    [INFO] Results:
    [INFO]
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO]
    [INFO]
    [INFO] --- maven-failsafe-plugin:3.2.2:verify (default) @ helloworld ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  2.984 s
    [INFO] Finished at: 2023-11-30T15:51:22+05:30
    [INFO] ------------------------------------------------------------------------