Chapter 5. Developing a hello world RESTful service application


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.

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 jboss-eap-ee-with-tools BOM and such dependencies are included with JBoss EAP.
    Note

    The dependency is defined without a version because jboss-eap-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 a 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.helloworld.rest;
    import jakarta.ws.rs.ApplicationPath;
    import jakarta.ws.rs.core.Application;
    
    @ApplicationPath("rest")                                    
    1
    
    public class JakartaRESTActivator extends Application {
        // Left empty intentionally
    }
    1
    Define the base URI for your REST resources. For example, if JBoss EAP 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.helloworld.rest;
    
    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 JBoss EAP 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] ------------------------------------------------------------------------
    ...

You can deploy your application on a JBoss EAP server running on bare metal or on OpenShift Container Platform.

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

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

To deploy your application on a JBoss EAP server running on OpenShift Container Platform, follow these procedures:

You can deploy an application to JBoss EAP by using the JBoss EAP 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 JBoss EAP deploy plug-in.

    $ mvn package wildfly:deploy

Verification

You can package the application with a provisioned server.

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 profile "provisioned-server" in the pom.xml configuration file. This profile configures the JBoss EAP Maven plug-in for provisioning of a server with the application deployed.

    <profile>
        <id>provisioned-server</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jboss.eap.plugins</groupId>
                    <artifactId>eap-maven-plugin</artifactId>
                    <configuration>
                        <channels>
                            <channel>
                                <manifest>
                                    <groupId>org.jboss.eap.channels</groupId>
                                    <artifactId>eap-8.1</artifactId>
                                </manifest>
                            </channel>
                        </channels>
                        <feature-packs>
                            <feature-pack>
                                <location>org.jboss.eap:wildfly-ee-galleon-pack</location>
                            </feature-pack>
                        </feature-packs>
                        <layers>
                            <layer>cloud-server</layer>
                            <layer>jaxrs</layer>
                        </layers>
                        <galleon-options>
                            <jboss-fork-embedded>true</jboss-fork-embedded>
                        </galleon-options>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>package</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
  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 JBoss EAP provisioned server based on the "provisioned-server" profile, use the JBoss EAP Maven plug-in start goal.

      $ mvn wildfly:start

Verification

You can use the source-to-image (S2I) workflow to deploy your applications to JBoss EAP on OpenShift Container Platform. 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 JBoss EAP server.

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 JBoss EAP 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 helloworld-rs/ <your_git_repo>
  2. Create a profile "openshift" in the pom.xml configuration file.

    This profile configures the JBoss EAP maven plug-in, with respect to deployment on OpenShift Container Platform.

    <profile>
        <id>openshift</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jboss.eap.plugins</groupId>
                    <artifactId>eap-maven-plugin</artifactId>
                    <configuration>
                        <channels>
                            <channel>
                                <manifest>
                                    <groupId>org.jboss.eap.channels</groupId>
                                    <artifactId>eap-8.1</artifactId>
                                </manifest>
                            </channel>
                        </channels>
                        <feature-packs>
                            <feature-pack>
                                <location>org.jboss.eap:wildfly-ee-galleon-pack</location>
                            </feature-pack>
                            <feature-pack>
                                <location>org.jboss.eap.cloud:eap-cloud-galleon-pack</location>
                            </feature-pack>
                        </feature-packs>
                        <layers>
                            <layer>cloud-server</layer>
                            <layer>jaxrs</layer>
                        </layers>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>package</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
                <!-- do not attach sources to openshift deployments -->
                <plugin>
                    <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>
    eap-maven-plugin is a JBoss EAP plug-in for provisioning a JBoss EAP instance, with the application deployed on OpenShift Container Platform.
    feature-packs defines the feature-packs (zipped files that contains features to dynamically provision a server). In this case we need the feature-packs org.jboss.eap:wildfly-ee-galleon-pack and org.jboss.eap.cloud:eap-cloud-galleon-pack.
    layers defines the layers (from the configured feature-packs) to include in the provisioned server. Each layer identifies one or more server capabilities that can be installed on its own, or in combination with other layers. In our case we opt for the cloud-server layer, which provisions just the basic features of JBoss EAP, well suited for a cloud server.
  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.

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

Prerequisites

Procedure

  1. Create a directory called charts in the application root directory 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 JBoss EAP repository in Helm.

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

      $ helm repo add jboss-eap https://jbossas.github.io/eap-charts/
    • If you already have added the JBoss EAP repository to Helm, update it.

      $ helm repo update jboss-eap
  4. Deploy the application using helm.

    $ helm install helloworld-rs -f helm.yaml jboss-eap/eap81

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 Application URL of the deployment in a browser.

    You are redirected to the servlet at path "/rest/HelloWorld" and you get the following message:

    Hello World!

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

To add tests for an application deployed on a JBoss EAP server running on bare metal, follow these procedures:

To add tests for an application deployed on a provisioned JBoss EAP server, follow these procedures:

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

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>**/*IT.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.

Create an integration test that verifies that the hello world RESTful service returns 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 that tests the deployment.

    package org.jboss.as.quickstarts.helloworld.rest;
    
    import jakarta.ws.rs.client.Client;
    import jakarta.ws.rs.client.ClientBuilder;
    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";
    
        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() {
            String responseMessage = client.target(getServerHost())
                    .path("helloworld-rs/rest/HelloWorld")
                    .request(MediaType.TEXT_PLAIN).get(String.class);
            assertEquals("Hello World!", responseMessage);
        }
    }

Test the application deployed on JBoss EAP 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-rs ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  9.982 s
    [INFO] Finished at: 2023-11-22T14:53:54+05:30
    [INFO] ------------------------------------------------------------------------

Test an application deployed on a provisioned server.

Prerequisites

Procedure

  1. Navigate to the <application_home> directory.
  2. 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="http://localhost:8080"
    Note

    The tests use SSL/TLS to connect to the deployed application. Therefore, you need the certificates to be trusted by the machine the tests are run from.

    To trust the certificates, you must add it to a Java trust store.

    Example

    $ keytool -trustcacerts -keystore <path-to-java-truststore> -storepass <trust-store-password> -importcert -alias <alias-for-the-certificate> -file <path-to-certificate>/<certificate-name>

    Example output

    [INFO] Running org.jboss.as.quickstarts.helloworld.rest.RESTEndPointIT
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.436 s -- in org.jboss.as.quickstarts.helloworld.rest.RESTEndPointIT
    [INFO]
    [INFO] Results:
    [INFO]
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    [INFO]
    [INFO]
    [INFO] --- failsafe:3.2.2:verify (default) @ helloworld-rs ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  2.646 s
    [INFO] Finished at: 2024-11-12T00:13:23+05:30
    [INFO] ------------------------------------------------------------------------

Test the application deployed to JBoss EAP on OpenShift Container Platform.

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.rest.RESTEndPointIT
    [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.345 s -- in org.jboss.as.quickstarts.helloworld.rest.RESTEndPointIT
    [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] ------------------------------------------------------------------------

Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat Documentation

Legal Notice

Theme

© 2026 Red Hat
Back to top