Chapter 4. Develop Eclipse MicroProfile Applications for JBoss EAP
4.1. Maven and the JBoss EAP Eclipse MicroProfile Maven repository Copy linkLink copied to clipboard!
4.1.1. Downloading the JBoss EAP Eclipse MicroProfile Maven repository patch as an archive file Copy linkLink copied to clipboard!
Whenever an Eclipse MicroProfile Expansion Pack is released for JBoss EAP, a corresponding patch is provided for the JBoss EAP Eclipse MicroProfile Maven repository. This patch is provided as an incremental archive file that is extracted into the existing Red Hat JBoss Enterprise Application Platform 7.3.0.GA Maven repository. The incremental archive file does not overwrite or remove any existing files, so there is no rollback requirement.
Prerequisites
- You have set up an account on the Red Hat Customer Portal.
Procedure
- Open a browser and log in to the Red Hat Customer Portal.
- Select Downloads from the menu at the top of the page.
- Find the Red Hat JBoss Enterprise Application Platform entry in the list and select it.
- From the Product drop-down list, select JBoss EAP XP.
- From the Version drop-down list, select 2.0.0.
- Click the Releases tab.
- Find JBoss EAP XP 2.0.0 Incremental Maven Repository in the list, and then click Download.
- Save the archive file to your local directory.
Additional Resources
- To learn more about the JBoss EAP Maven repository, see About the Maven Repository in the JBoss EAP Development Guide.
4.1.2. Applying the JBoss EAP Eclipse MicroProfile Maven repository patch on your local system Copy linkLink copied to clipboard!
You can install the JBoss EAP Eclipse MicroProfile Maven repository patch on your local file system.
When you apply a patch in the form of an incremental archive file to the repository, new files are added to this repository. The incremental archive file does not overwrite or remove any existing files on the repository, so there is no rollback requirement.
Prerequisites
You have downloaded and installed the Red Hat JBoss Enterprise Application Platform 7.3.0.GA Maven repository on your local system.
- Check that you have this minor version of the Red Hat JBoss Enterprise Application Platform 7.3 Maven repository installed on your local system.
- You have downloaded the JBoss EAP XP 2.0.0 Incremental Maven repository on your local system.
Procedure
-
Locate the path to your Red Hat JBoss Enterprise Application Platform 7.3.0.GA Maven repository. For example,
/path/to/repo/jboss-eap-7.3.0.GA-maven-repository/maven-repository/
. Extract the downloaded JBoss EAP XP 2.0.0 Incremental Maven repository directly into the directory of the Red Hat JBoss Enterprise Application Platform 7.3.0.GA Maven repository. For example, open a terminal and issue the following command, replacing the value for your Red Hat JBoss Enterprise Application Platform 7.3.0.GA Maven repository path:
unzip -o jboss-eap-xp-2.0.0-incremental-maven-repository.zip -d EAP_MAVEN_REPOSITORY_PATH
$ unzip -o jboss-eap-xp-2.0.0-incremental-maven-repository.zip -d EAP_MAVEN_REPOSITORY_PATH
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The EAP_MAVEN_REPOSITORY_PATH points to the jboss-eap-7.3.0.GA-maven-repository
. For example, this procedure demonstrated the use of the path /path/to/repo/jboss-eap-7.3.0.GA-maven-repository/
.
After you extract the JBoss EAP XP Incremental Maven repository into the Red Hat JBoss Enterprise Application Platform 7.3.0.GA Maven repository, the repository name becomes JBoss EAP Eclipse MicroProfile Maven repository.
Additional Resources
- To determine the URL of the JBoss EAP Maven repository, see Determining the URL for the JBoss EAP Maven repository in the JBoss EAP Development Guide.
4.1.3. Supported JBoss EAP Eclipse MicroProfile BOM Copy linkLink copied to clipboard!
JBoss EAP XP 2.0.0 includes the JBoss EAP Eclipse MicroProfile BOM. This BOM is named jboss-eap-xp-microprofile
, and its use case supports JBoss EAP Eclipse MicroProfile APIs.
BOM Artifact ID | Use Case |
---|---|
jboss-eap-xp-microprofile |
This BOM, whose |
4.1.4. Using the JBoss EAP Eclipse MicroProfile Maven repository Copy linkLink copied to clipboard!
You can access the jboss-eap-xp-microprofile
BOM after you install the Red Hat JBoss Enterprise Application Platform 7.3.0.GA Maven repository and apply the JBoss EAP XP Incremental Maven repository to it. The repository name then becomes JBoss EAP Eclipse MicroProfile Maven repository. The BOM is shipped inside the JBoss EAP XP Incremental Maven repository.
You must configure one of the following to use the JBoss EAP Eclipse MicroProfile Maven repository:
- The Maven global or user settings
- The project’s POM files
Maven settings used with a repository manager or repository on a shared server provide better control and manageability of projects.
You can use an alternative mirror to redirect all lookup requests for a specific repository to your repository manager without changing the project files.
Configuring the JBoss EAP Eclipse MicroProfile Maven repository by modifying the POM file overrides the global and user Maven settings for the configured project.
Prerequisites
- You have installed the Red Hat JBoss Enterprise Application Platform 7.3 Maven repository on your local system, and you have applied the JBoss EAP XP Incremental Maven repository to it.
Procedure
- Choose a configuration method and configure the JBoss EAP Eclipse MicroProfile Maven repository.
After you have configured the JBoss EAP Eclipse MicroProfile Maven repository, add the
jboss-eap-xp-microprofile
BOM to the project POM file. The following example shows how to configure the BOM in the<dependencyManagement>
section of thepom.xml
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf you do not specify a value for the
type
element in thepom.xml
file, Maven specifies ajar
value for the element.
Additional Resources
- For more information about selecting methods to configure the JBoss EAP Maven repository, see Use the Maven Repository in the JBoss EAP Development Guide.
- For more information about managing dependencies, see Dependency Management.
4.2. Eclipse MicroProfile Config development Copy linkLink copied to clipboard!
4.2.1. Creating a Maven project for Eclipse MicroProfile Config Copy linkLink copied to clipboard!
Create a Maven project with the required dependencies and the directory structure for creating an Eclipse MicroProfile Config application.
Prerequisites
- Maven is installed.
Procedure
Set up the Maven project.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This creates the directory structure for the project and
pom.xml
configuration file.To let the POM file automatically manage the versions for the Eclipse MicroProfile Config artifact and the Eclipse MicroProfile REST Client artifact in the
jboss-eap-xp-microprofile
BOM, import the BOM to the<dependencyManagement>
section of the project POM file.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the Eclipse MicroProfile Config artifact and the Eclipse MicroProfile REST Client artifact and other dependencies, managed by the BOM, to the
<dependency>
section of the project POM file. The following example demonstrates adding the Eclipse MicroProfile Config and the Eclipse MicroProfile REST Client dependencies to the file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.2.2. Using MicroProfile Config property in an application Copy linkLink copied to clipboard!
Create an application that uses a configured ConfigSource
.
Prerequisites
- Eclipse MicroProfile Config is enabled in JBoss EAP.
- The latest POM is installed.
- The Maven project is configured for creating an Eclipse MicroProfile Config application.
Procedure
Create the directory to store class files:
mkdir -p APPLICATION_ROOT/src/main/java/com/example/microprofile/config/
$ mkdir -p APPLICATION_ROOT/src/main/java/com/example/microprofile/config/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where
APPLICATION_ROOT
is the directory containing thepom.xml
configuration file for the application.Navigate to the new directory:
cd APPLICATION_ROOT/src/main/java/com/example/microprofile/config/
$ cd APPLICATION_ROOT/src/main/java/com/example/microprofile/config/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create all class files described in this procedure in this directory.
Create a class file named
HelloApplication.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This class defines the application as a JAX-RS application.
Create a class file named
HelloService.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a class file named
HelloWorld.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- A MicroProfile Config property is injected into the class with the annotation
@ConfigProperty(name="name", defaultValue="jim")
. If noConfigSource
is configured, the valuejim
is returned.
Create an empty file named
beans.xml
in thesrc/main/webapp/WEB-INF/
directory:touch APPLICATION_ROOT/src/main/webapp/WEB-INF/beans.xml
$ touch APPLICATION_ROOT/src/main/webapp/WEB-INF/beans.xml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where
APPLICATION_ROOT
is the directory containing thepom.xml
configuration file for the application.Navigate to the root directory of the application:
cd APPLICATION_ROOT
$ cd APPLICATION_ROOT
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where
APPLICATION_ROOT
is the directory containing thepom.xml
configuration file for the application.Build the project:
mvn clean install wildfly:deploy
$ mvn clean install wildfly:deploy
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Test the output:
curl http://localhost:8080/microprofile-config/config/json
$ curl http://localhost:8080/microprofile-config/config/json
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The following is the expected output:
{"result":"Hello jim"}
{"result":"Hello jim"}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.3. Eclipse MicroProfile Fault Tolerance application development Copy linkLink copied to clipboard!
4.3.1. Adding the MicroProfile Fault Tolerance extension Copy linkLink copied to clipboard!
The MicroProfile Fault Tolerance extension is included in standalone-microprofile.xml
and standalone-microprofile-ha.xml
configurations that are provided as part of JBoss EAP XP.
The extension is not included in the standard standalone.xml
configuration. To use the extension, you must manually enable it.
Prerequisites
- EAP XP pack is installed.
Procedure
Add the MicroProfile Fault Tolerance extension using the following management CLI command:
/extension=org.wildfly.extension.microprofile.fault-tolerance-smallrye:add
/extension=org.wildfly.extension.microprofile.fault-tolerance-smallrye:add
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enable the
microprofile-fault-tolerance-smallrye
subsystem using the following managenent command:/subsystem=microprofile-fault-tolerance-smallrye:add
/subsystem=microprofile-fault-tolerance-smallrye:add
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Reload the server with the following management command:
reload
reload
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.3.2. Configuring Maven project for Eclipse MicroProfile Fault Tolerance Copy linkLink copied to clipboard!
Create a Maven project with the required dependencies and the directory structure for creating an Eclipse MicroProfile Fault Tolerance application.
Prerequisites
- Maven is installed.
Procedure
Set up the Maven project:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The command creates the directory structure for the project and the
pom.xml
configuration file.To let the POM file automatically manage the versions for the Eclipse MicroProfile Fault Tolerance artifact in the
jboss-eap-xp-microprofile
BOM, import the BOM to the<dependencyManagement>
section of the project POM file.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace ${version.microprofile.bom} with the installed version of BOM.
Add the Eclipse MicroProfile Fault Tolerance artifact, managed by the BOM, to the
<dependency>
section of the project POM file. The following example demonstrates adding the Eclipse MicroProfile Fault Tolerance dependency to the file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.3.3. Creating a fault tolerant application Copy linkLink copied to clipboard!
Create a fault-tolerant application that implements retry, timeout, and fallback patterns for fault tolerance.
Prerequisites
- Maven dependencies have been configured.
Procedure
Create the directory to store class files:
mkdir -p APPLICATION_ROOT/src/main/java/com/example/microprofile/faulttolerance
$ mkdir -p APPLICATION_ROOT/src/main/java/com/example/microprofile/faulttolerance
Copy to Clipboard Copied! Toggle word wrap Toggle overflow APPLICATION_ROOT is the directory containing the
pom.xml
configuration file for the application.Navigate to the new directory:
cd APPLICATION_ROOT/src/main/java/com/example/microprofile/faulttolerance
$ cd APPLICATION_ROOT/src/main/java/com/example/microprofile/faulttolerance
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For the following steps, create all class files in the new directory.
Create a simple entity representing a coffee sample as
Coffee.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a class file
CoffeeApplication.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a CDI Bean as
CoffeeRepositoryService.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a class file
CoffeeResource.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Navigate to the root directory of the application:
cd APPLICATION_ROOT
$ cd APPLICATION_ROOT
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Build the application using the following Maven command:
mvn clean install wildfly:deploy
$ mvn clean install wildfly:deploy
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Access the application at
http://localhost:8080/microprofile-fault-tolerance/coffee
.
Additional Resources
-
For a detailed example of fault tolerant application, which includes artificial failures to test the fault tolerance of the application, see the
microprofile-fault-tolerance
quickstart.
4.4. Eclipse MicroProfile Health development Copy linkLink copied to clipboard!
4.4.1. Custom health check example Copy linkLink copied to clipboard!
The default implementation provided by the microprofile-health-smallrye
subsystem performs a basic health check. For more detailed information, on either the server or application status, custom health checks may be included. Any CDI beans that include the org.eclipse.microprofile.health.Health
annotation at the class level are automatically discovered and invoked at runtime.
The following example demonstrates how to create a new implementation of a health check that returns an UP
state.
Once deployed, any subsequent health check queries include the custom checks, as demostrated in the following example.
4.4.2. The @Liveness annotation example Copy linkLink copied to clipboard!
The following is an example of using the @Liveness
annotation in an application.
4.4.3. The @Readiness annotation example Copy linkLink copied to clipboard!
The following example demonstrates checking connection to a database. If the database is down, the readiness check reports error.
4.5. Eclipse MicroProfile JWT application development Copy linkLink copied to clipboard!
4.5.1. Enabling microprofile-jwt-smallrye subsystem Copy linkLink copied to clipboard!
The Eclipse MicroProfile JWT integration is provided by the microprofile-jwt-smallrye
subsystem and is included in the default configuration. If the subsystem is not present in the default configuration, you can add it as follows.
Prerequisites
- EAP XP is installed.
Procedure
Enable the MicroProfile JWT smallrye extension in JBoss EAP:
/extension=org.wildfly.extension.microprofile.jwt-smallrye:add
/extension=org.wildfly.extension.microprofile.jwt-smallrye:add
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enable the
microprofile-jwt-smallrye
subsystem:/subsystem=microprofile-jwt-smallrye:add
/subsystem=microprofile-jwt-smallrye:add
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Reload the server:
reload
reload
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The microprofile-jwt-smallrye
subsystem is enabled.
4.5.2. Configuring Maven project for developing JWT applications Copy linkLink copied to clipboard!
Create a Maven project with the required dependencies and the directory structure for developing a JWT application.
Prerequisites
- Maven is installed.
-
microprofile-jwt-smallrye
subsystem is enabled.
Procedure
Set up the maven project:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The command creates the directory structure for the project and the
pom.xml
configuration file.To let the POM file automatically manage the versions for the Eclipse MicroProfile JWT artifact in the
jboss-eap-xp-microprofile
BOM, import the BOM to the<dependencyManagement>
section of the project POM file.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace ${version.microprofile.bom} with the installed version of BOM.
Add the Eclipse MicroProfile JWT artifact, managed by the BOM, to the
<dependency>
section of the project POM file. The following example demonstrates adding the Eclipse MicroProfile JWT dependency to the file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.5.3. Creating an application with Eclipse MicroProfile JWT Copy linkLink copied to clipboard!
Create an application that authenticates requests based on JWT tokens and implements authorization based on the identity of the token bearer.
The following procedure provides code for generating tokens as an example. You should implement your own token generator.
Prerequisites
- Maven project is configured with the correct dependencies.
Procedure
Create a token generator.
This step serves as a reference. For a production environment, implement your own token generator.
Create a directory
src/test/java
for token the generator utility and navigate to it:mkdir -p src/test/java cd src/test/java
$ mkdir -p src/test/java $ cd src/test/java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a class file
TokenUtil.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Create the
web.xml
file in thesrc/main/webapp/WEB-INF
directory with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a class file
SampleEndPoint.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The methods annotated with
@Path
are the JAX-RS endpoints.The annotation
@Claim
defines a JWT claim.Create a class file
App.java
to enable JAX-RS:Copy to Clipboard Copied! Toggle word wrap Toggle overflow The annotation
@LoginConfig(authMethod="MP-JWT", realmName="MP JWT Realm")
enables JWT RBAC during deployment.Compile the application with the following Maven command:
mvn package
$ mvn package
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Generate JWT token using the token generator utility:
mvn exec:java -Dexec.mainClass=org.wildfly.quickstarts.mpjwt.TokenUtil -Dexec.classpathScope=test -Dexec.args="testUser 2017-09-15 Echoer Subscriber"
$ mvn exec:java -Dexec.mainClass=org.wildfly.quickstarts.mpjwt.TokenUtil -Dexec.classpathScope=test -Dexec.args="testUser 2017-09-15 Echoer Subscriber"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Build and deploy the application using the following Maven command:
mvn package wildfly:deploy
$ mvn package wildfly:deploy
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Test the application.
Call the
Sample/subscription
endpoint using the bearer token:curl -H "Authorization: Bearer ey..rg" http://localhost:8080/microprofile-jwt/rest/Sample/subscription
$ curl -H "Authorization: Bearer ey..rg" http://localhost:8080/microprofile-jwt/rest/Sample/subscription
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Call the
Sample/birthday
endpoint:curl -H "Authorization: Bearer ey..rg" http://localhost:8080/microprofile-jwt/rest/Sample/birthday
$ curl -H "Authorization: Bearer ey..rg" http://localhost:8080/microprofile-jwt/rest/Sample/birthday
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.6. Eclipse MicroProfile Metrics development Copy linkLink copied to clipboard!
4.6.1. Creating an Eclipse MicroProfile Metrics application Copy linkLink copied to clipboard!
Create an application that returns the number of requests made to the application.
Procedure
Create a class file
HelloService.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a class file
HelloWorld.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Update the
pom.xml
file to include the following dependency:<dependency> <groupId>org.eclipse.microprofile.metrics</groupId> <artifactId>microprofile-metrics-api</artifactId> <scope>provided</scope> </dependency>
<dependency> <groupId>org.eclipse.microprofile.metrics</groupId> <artifactId>microprofile-metrics-api</artifactId> <scope>provided</scope> </dependency>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Build the application using the following Maven command:
mvn clean install wildfly:deploy
$ mvn clean install wildfly:deploy
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Test the metrics:
Issue the following command in the CLI:
curl -v http://localhost:9990/metrics | grep request_count | grep helloworld-rs-metrics
$ curl -v http://localhost:9990/metrics | grep request_count | grep helloworld-rs-metrics
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Expected output:
jboss_undertow_request_count_total{deployment="helloworld-rs-metrics.war",servlet="org.jboss.as.quickstarts.rshelloworld.JAXActivator",subdeployment="helloworld-rs-metrics.war",microprofile_scope="vendor"} 0.0
jboss_undertow_request_count_total{deployment="helloworld-rs-metrics.war",servlet="org.jboss.as.quickstarts.rshelloworld.JAXActivator",subdeployment="helloworld-rs-metrics.war",microprofile_scope="vendor"} 0.0
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - In a browser, navigate to the URL http://localhost:8080/helloworld-rs/rest/json.
Re-Issue the following command in the CLI:
curl -v http://localhost:9990/metrics | grep request_count | grep helloworld-rs-metrics
$ curl -v http://localhost:9990/metrics | grep request_count | grep helloworld-rs-metrics
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Expected output:
jboss_undertow_request_count_total{deployment="helloworld-rs-metrics.war",servlet="org.jboss.as.quickstarts.rshelloworld.JAXActivator",subdeployment="helloworld-rs-metrics.war",microprofile_scope="vendor"} 1.0
jboss_undertow_request_count_total{deployment="helloworld-rs-metrics.war",servlet="org.jboss.as.quickstarts.rshelloworld.JAXActivator",subdeployment="helloworld-rs-metrics.war",microprofile_scope="vendor"} 1.0
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.7. Developing an Eclipse MicroProfile OpenAPI application Copy linkLink copied to clipboard!
4.7.1. Enabling Eclipse MicroProfile OpenAPI Copy linkLink copied to clipboard!
The microprofile-openapi-smallrye
subsystem is provided in the standalone-microprofile.xml
configuration. However, JBoss EAP XP uses the standalone.xml
by default. You must include the subsystem in standalone.xml
to use it.
Alternatively, you can follow the procedure Updating standalone configurations with Eclipse MicroProfile subsystems and extensions to update the standalone.xml
configuration file.
Procedure
Enable the MicroProfile OpenAPI smallrye extension in JBoss EAP:
/extension=org.wildfly.extension.microprofile.openapi-smallrye:add()
/extension=org.wildfly.extension.microprofile.openapi-smallrye:add()
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enable the
microprofile-openapi-smallrye
subsystem using the following management command:/subsystem=microprofile-openapi-smallrye:add()
/subsystem=microprofile-openapi-smallrye:add()
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Reload the server.
reload
reload
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The microprofile-openapi-smallrye
subsystem is enabled.
4.7.2. Configuring Maven project for Eclipse MicroProfile OpenAPI Copy linkLink copied to clipboard!
Create a Maven project to set up the dependencies for creating an Eclipse MicroProfile OpenAPI application.
Prerequisites
- Maven is installed.
- JBoss EAP Maven repository is configured.
Procedure
Initialize the project:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The command creates the directory structure for the project and the
pom.xml
configuration file.Edit the
pom.xml
configuration file to contain:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
pom.xml
configuration file and directory structure to create an application.
4.7.3. Creating an Eclipse MicroProfile OpenAPI application Copy linkLink copied to clipboard!
Create an application that returns an OpenAPI v3 document.
Prerequisites
- Maven project is configured for creating an Eclipse MicroProfile OpenAPI application.
Procedure
Create the directory to store class files:
mkdir -p APPLICATION_ROOT/src/main/java/com/example/microprofile/openapi/
$ mkdir -p APPLICATION_ROOT/src/main/java/com/example/microprofile/openapi/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow APPLICATION_ROOT is the directory containing the
pom.xml
configuration file for the application.Navigate to the new directory:
cd APPLICATION_ROOT/src/main/java/com/example/microprofile/openapi/
$ cd APPLICATION_ROOT/src/main/java/com/example/microprofile/openapi/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow All the class files in the following steps must be created in this directory.
Create the class file
InventoryApplication.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This class serves as the REST endpoint for the application.
Create a class file
Fruit.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a class file
FruitResource.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Navigate to the root directory of the application:
cd APPLICATION_ROOT
$ cd APPLICATION_ROOT
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Build and deploy the application using the following Maven command:
mvn wildfly:deploy
$ mvn wildfly:deploy
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Test the application.
Access the OpenAPI documentation of the sample application using
curl
:curl http://localhost:8080/openapi
$ curl http://localhost:8080/openapi
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The following output is returned:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional Resources
- For a list of annotations defined in MicroProfile SmallRye OpenAPI, see MicroProfile OpenAPI annotations.
4.7.4. Configuring JBoss EAP to serve a static OpenAPI document Copy linkLink copied to clipboard!
Configure JBoss EAP to serve a static OpenAPI document that describes the REST services for the host.
When JBoss EAP is configured to serve a static OpenAPI document, the static OpenAPI document is processed before any JAX-RS and MicroProfile OpenAPI annotations.
In a production environment, disable annotation processing when serving a static document. Disabling annotation processing ensures that an immutable and versioned API contract is available for clients.
Procedure
Create a directory in the application source tree:
mkdir APPLICATION_ROOT/src/main/webapp/META-INF
$ mkdir APPLICATION_ROOT/src/main/webapp/META-INF
Copy to Clipboard Copied! Toggle word wrap Toggle overflow APPLICATION_ROOT is the directory containing the
pom.xml
configuration file for the application.Query the OpenAPI endpoint, redirecting the output to a file:
curl http://localhost:8080/openapi?format=JSON > src/main/webapp/META-INF/openapi.json
$ curl http://localhost:8080/openapi?format=JSON > src/main/webapp/META-INF/openapi.json
Copy to Clipboard Copied! Toggle word wrap Toggle overflow By default, the endpoint serves a YAML document,
format=JSON
specifies that a JSON document is returned.Configure the application to skip annotation scanning when processing the OpenAPI document model:
echo "mp.openapi.scan.disable=true" > APPLICATION_ROOT/src/main/webapp/META-INF/microprofile-config.properties
$ echo "mp.openapi.scan.disable=true" > APPLICATION_ROOT/src/main/webapp/META-INF/microprofile-config.properties
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Rebuild the application:
mvn clean install
$ mvn clean install
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy the application again using the following management CLI commands:
Undeploy the application:
undeploy microprofile-openapi.war
undeploy microprofile-openapi.war
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy the application:
deploy APPLICATION_ROOT/target/microprofile-openapi.war
deploy APPLICATION_ROOT/target/microprofile-openapi.war
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
JBoss EAP now serves a static OpenAPI document at the OpenAPI endpoint.
4.8. Eclipse MicroProfile REST Client development Copy linkLink copied to clipboard!
4.8.1. A comparison between MicroProfile REST client and JAX-RS syntaxes Copy linkLink copied to clipboard!
The MicroProfile REST client enables a version of distributed object communication, which is also implemented in CORBA, Java Remote Method Invocation (RMI), the JBoss Remoting Project, and RESTEasy. For example, consider the resource:
The following example demonstrates using the JAX-RS native way to access the TestResource
class:
Client client = ClientBuilder.newClient(); String response = client.target("http://localhost:8081/test").request().get(String.class);
Client client = ClientBuilder.newClient();
String response = client.target("http://localhost:8081/test").request().get(String.class);
However, Microprofile REST client supports a more intuitive syntax by directly calling the test()
method as the following example demonstrates:
In the preceding example, making calls on the TestResource
class becomes much easier with the TestResourceIntf
class, as illustrated by the call service.test()
.
The following example is a more elaborate version of the TestResourceIntf
class:
Calling the service.test("p", "q", "e")
method results in an HTTP message as shown in the following example:
4.8.2. Programmatic registration of providers in MicroProfile REST client Copy linkLink copied to clipboard!
With the MicroProfile REST client, you can configure the client environment by registering providers. For example:
TestResourceIntf service = RestClientBuilder.newBuilder() .baseUrl(http://localhost:8081/)) .register(MyClientResponseFilter.class) .register(MyMessageBodyReader.class) .build(TestResourceIntf.class);
TestResourceIntf service = RestClientBuilder.newBuilder()
.baseUrl(http://localhost:8081/))
.register(MyClientResponseFilter.class)
.register(MyMessageBodyReader.class)
.build(TestResourceIntf.class);
4.8.3. Declarative registration of providers in MicroProfile REST client Copy linkLink copied to clipboard!
Use the MicroProfile REST client to register providers declaratively by adding the org.eclipse.microprofile.rest.client.annotation.RegisterProvider
annotation to the target interface, as shown in the following example:
Declaring the MyClientResponseFilter
class and the MyMessageBodyReader
class with annotations eliminates the need to call the RestClientBuilder.register()
method.
4.8.4. Declarative specification of headers in MicroProfile REST client Copy linkLink copied to clipboard!
You can specify a header for an HTTP request in the following ways:
- By annotating one of the resource method parameters.
-
By declaratively using the
org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam
annotation.
The following example illustrates setting a header by annotating one of the resource method parameters with the annotation @HeaderValue
:
@POST @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.TEXT_PLAIN) String contentLang(@HeaderParam(HttpHeaders.CONTENT_LANGUAGE) String contentLanguage, String subject);
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
String contentLang(@HeaderParam(HttpHeaders.CONTENT_LANGUAGE) String contentLanguage, String subject);
The following example illustrates setting a header using the org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam
annotation:
4.8.5. Propagation of headers on the server in MicroProfile REST client Copy linkLink copied to clipboard!
An instance of org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory
, if activated, can do a bulk transfer of incoming headers to an outgoing request. The default instance org.eclipse.microprofile.rest.client.ext.DefaultClientHeadersFactoryImpl
returns a map consisting of those incoming headers that are listed in the comma-separated configuration property org.eclipse.microprofile.rest.client.propagateHeaders
.
The following are the rules for instantiating the ClientHeadersFactory
interface:
-
A
ClientHeadersFactory
instance invoked in the context of a JAX-RS request can support injection of fields and methods annotated with@Context
. -
A
ClientHeadersFactory
instance that is managed by CDI must use the appropriate CDI-managed instance. It must also support the@Inject
injection.
The org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory
interface is defined as follows:
Additional resources
4.8.6. ResponseExceptionMapper in MicroProfile REST client Copy linkLink copied to clipboard!
The org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper
class is the client-side inverse of the javax.ws.rs.ext.ExceptionMapper
class, which is defined in JAX-RS. The ExceptionMapper.toResponse()
method turns an Exception
class thrown during the server-side processing into a Response
class. The ResponseExceptionMapper.toThrowable()
method turns a Response
class received on the client-side with an HTTP error status into an Exception
class.
You can register the ResponseExceptionMapper
class either programmatically or declaratively. In the absence of a registered ResponseExceptionMapper
class, a default ResponseExceptionMapper
class maps any response with status >= 400
to a WebApplicationException
class.
4.8.7. Context dependency injection with MicroProfile REST client Copy linkLink copied to clipboard!
In MicroProfile REST client, you must annotate any interface that is managed as a CDI bean with the @RegisterRestClient
class. For example:
Here, the MicroProfile REST client implementation creates a client for a TestDataBase
class service, allowing easy access by the TestResourceImpl
class. However, it does not include the information about the path to the TestDataBase
class implementation. This information can be supplied by the optional @RegisterProvider
parameter baseUri
:
This indicates that you can access the implementation of TestDataBase
at https://localhost:8080/webapp. You can also supply the information externally with the following system variable:
<fully qualified name of TestDataBase>/mp-rest/url=<URL>
<fully qualified name of TestDataBase>/mp-rest/url=<URL>
For example, the following command indicates that you can access an implementation of the com.bluemonkeydiamond.TestDatabase
class at https://localhost:8080/webapp:
com.bluemonkeydiamond.TestDatabase/mp-rest/url=https://localhost:8080/webapp
com.bluemonkeydiamond.TestDatabase/mp-rest/url=https://localhost:8080/webapp