Chapter 5. Developing a hello world RESTful service application
5.1. Creating a Hello World REST service Copy linkLink copied to clipboard!
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
You have created a Maven project.
To follow along with the example in the procedure, set the value of artifactID to
helloworld-rs.For more information, see Creating a Maven project for a JBoss EAP application.
Procedure
Add the required dependency to
pom.xmlconfiguration 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.rsdependency provides Jakarta RESTful Web Services API.- 2
- Define the scope as
providedso 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 thejboss-eap-ee-with-toolsBOM and such dependencies are included with JBoss EAP.
NoteThe dependency is defined without a version because
jboss-eap-ee-with-toolsBOM was imported in the<dependencyManagement>section.- Navigate to the <application_home> directory.
Create a directory to store the Java files.
$ mkdir -p src/main/java/org/jboss/as/quickstarts/helloworldNavigate to the new directory.
$ cd src/main/java/org/jboss/as/quickstarts/helloworldCreate a Java class
JakartaRESTActivator.javathat extends thejakarta.ws.rs.core.Applicationclass.The reason for extending the
jakarta.ws.rs.core.Applicationclass is that to define the base URI for your service, you must define the path by using the@ApplicationPathannotation, which can only be applied to a subclass of thejakarta.ws.rs.core.Applicationclass.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 behttp://localhost:8080/helloworld-rs/rest/
Create a Java class
HelloWorld.javathat 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 { @GET2 @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
@GETannotation 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.
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.
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>-
Rename the file to
index.html. - Navigate to the <application_home> directory.
Compile and package the application as a web archive (WAR) with the following command:
$ mvn packageExample output
... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ ...
5.2. Building and deploying a RESTful application to the server Copy linkLink copied to clipboard!
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:
5.2.1. Building and deploying a RESTful application to a bare metal installation Copy linkLink copied to clipboard!
You can deploy an application to JBoss EAP by using the JBoss EAP deploy plug-in.
Prerequisites
You have created an application.
For more information, see Creating a Hello World REST service.
- JBoss EAP is running.
Procedure
Navigate to the application root directory.
The application root directory contains the
pom.xmlconfiguration file.Add the following build configuration to the
pom.xmlconfiguration 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.
Build and deploy the application by using the JBoss EAP deploy plug-in.
$ mvn package wildfly:deploy
Verification
To access the application, navigate to the address
http://localhost:8080/helloworld-rs/in a browser.You are redirected to http://localhost:8080/helloworld-rs/rest/HelloWorld and you get the following message:
Hello World!
5.2.2. Building and deploying a RESTful application on a provisioned server Copy linkLink copied to clipboard!
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
You have created an application.
For more information, see Creating a Hello World REST service.
Procedure
- Navigate to the <application_home> directory.
Create a profile "provisioned-server" in the
pom.xmlconfiguration 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>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.
Provision the server with the application deployed.
$ mvn clean packageStart the server.
To start the JBoss EAP provisioned server based on the "provisioned-server" profile, use the JBoss EAP Maven plug-in
startgoal.$ mvn wildfly:start
Verification
Navigate to the address http://localhost:8080/helloworld-rs/rest/HelloWorld in a browser. You get the following message:
Hello World!
5.2.3. Building and deploying a RESTful application to OpenShift Copy linkLink copied to clipboard!
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.
5.2.3.1. Preparing a RESTFul application for deployment on OpenShift Copy linkLink copied to clipboard!
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
You have created an application.
For more information, see Creating a Hello World REST service.
- You have created a Git repository.
Procedure
Move the application to your local Git repository, if it already is not in it.
$ mv helloworld-rs/ <your_git_repo>Create a profile "openshift" in the
pom.xmlconfiguration 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-pluginis a JBoss EAP plug-in for provisioning a JBoss EAP instance, with the application deployed on OpenShift Container Platform.feature-packsdefines the feature-packs (zipped files that contains features to dynamically provision a server). In this case we need the feature-packsorg.jboss.eap:wildfly-ee-galleon-packandorg.jboss.eap.cloud:eap-cloud-galleon-pack.layersdefines 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 thecloud-serverlayer, which provisions just the basic features of JBoss EAP, well suited for a cloud server.
Verify that the applications compiles.
$ mvn package -PopenshiftClean the project so that the project contains only the sources.
$ mvn clean- Push the changes to your repository.
5.2.3.2. Building and deploying a RESTful application to server on OpenShift with Helm Copy linkLink copied to clipboard!
Use the JBoss EAP Helm chart to configure and deploy application to JBoss EAP on OpenShift with Helm.
Prerequisites
- You have prepared your application for deployment on OpenShift.
You have created a project in OpenShift.
For more information see Working with projects.
You have installed OpenShift CLI (
oc)For more information, see Installing the OpenShift CLI.
You are logged in to OpenShift from your machine.
For more information, see Logging in to the OpenShift CLI.
You have installed helm.
For more information, see Installing Helm.
Procedure
Create a directory called
chartsin the application root directory and navigate to it. Application root directory is the one that containspom.xmlconfiguration file.$ mkdir charts; cd chartsCreate a file
helm.yamlwith the following content:build: uri: https://github.com/<user>/<repository>.git1 ref: <branch_name>2 contextDir: helloworld-rs3 deploy: replicas: 14 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
Deploy the application using helm.
$ helm install helloworld-rs -f helm.yaml jboss-eap/eap81
Verification
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.
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!
5.3. Testing a RESTful application deployed on JBoss EAP Copy linkLink copied to clipboard!
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:
5.3.1. Adding the Maven dependencies and profile required for integration tests Copy linkLink copied to clipboard!
To create integration tests for your applications, add the required Maven dependencies.
Prerequisites
You have created a Maven project.
For more information, see Creating a Maven project for a Hello World application.
Procedure
Define the following properties in the
pom.xmlconfiguration file:<properties> ... <version.plugin.failsafe>3.2.2</version.plugin.failsafe> </properties>Add the dependency required for tests.
<project> ... <dependencies> ... <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> </project>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>
5.3.2. Creating a test class to test the hello world RESTful service Copy linkLink copied to clipboard!
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
You have deployed your RESTful application on JBoss EAP.
For more information, see Building and deploying an application to the server.
You have added the Maven dependencies required for tests.
For more information, see Adding the Maven dependencies and profile required for integration tests.
Procedure
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>Create a directory to store the test class.
$ mkdir -p <application_home>/src/test/java/org/jboss/as/quickstarts/helloworld/restNavigate to the new directory.
$ cd src/test/java/org/jboss/as/quickstarts/helloworld/restCreate a Java class
RESTEndPointITthat 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); } }
5.3.3. Testing a RESTful application deployed on JBoss EAP that is running on bare metal Copy linkLink copied to clipboard!
Test the application deployed on JBoss EAP that is running on bare metal.
Prerequisites
You have created a test class.
For more information, see Creating a test class to test the hello world RESTful service.
- The application to test is deployed on JBoss EAP.
- JBoss EAP is running.
Procedure
- Navigate to the <application_home> directory.
Run the integration test by using the
verifycommand with theintegration-testingprofile.$ mvn verify -Pintegration-testingExample 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] ------------------------------------------------------------------------
5.3.4. Testing a RESTFul application deployed on a provisioned server Copy linkLink copied to clipboard!
Test an application deployed on a provisioned server.
Prerequisites
You have deployed an application to a provisioned server.
For more information, see Creating a test class to test the hello world RESTful service.
Procedure
- Navigate to the <application_home> directory.
Run the test by using the
verifycommand, activating theintegration-testingprofile and specifying the URL to the application.$ mvn verify -Pintegration-testing -Dserver.host="http://localhost:8080"NoteThe 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] ------------------------------------------------------------------------
5.3.5. Testing a RESTful application deployed to JBoss EAP on OpenShift Copy linkLink copied to clipboard!
Test the application deployed to JBoss EAP on OpenShift Container Platform.
Prerequisites
You have created a test class.
For more information, see Creating a test class to test the hello world RESTful service.
Procedure
- Push the changes to your Git repository.
- Navigate to the <application_home> directory.
Run the test by using the
verifycommand, activating theintegration-testingprofile 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] ------------------------------------------------------------------------