3 ways to create Microprofile applications

Microprofile applications can be used in a large variety of contexts. In this tutorial we will learn how to use its API in the most common runtime environments.

Configuring Microprofile API with Thorntail

Thorntail offers a modern approach to packaging and running Java EE applications, including just enough of the environment to “java -jar” your application. Most interestingly, it’s full MicroProfile compatible.

In order to use Microprofile API, you can just plug in the Microprofile API needed by your project, leaving aside the version which is defined in the Thorntail’s BOM file. Here is the set of Microprofile dependencies you can include in your applications:

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>io.thorntail</groupId>
        <artifactId>bom-all</artifactId>
        <version>${version.thorntail}</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <finalName>demo</finalName>
    <plugins>
      <plugin>
        <groupId>io.thorntail</groupId>
        <artifactId>thorntail-maven-plugin</artifactId>
        <version>${version.thorntail}</version>
        
        <executions>
          <execution>
            <goals>
              <goal>package</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile-openapi</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile-jwt</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>opentracing</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile-fault-tolerance</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile-health</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile-restclient</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile-metrics</artifactId>
    </dependency>
  </dependencies>

Aside from that, you can use also an “umbrella” fraction named “microprofile” which brings into the game all the above fractions:

<dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile</artifactId>
</dependency>

In this tutorial Managing Microprofile Health Checks you can check how to use Microprofile Health extension on Thorntail and deploy it on Openshift:

Configuring Microprofile API with Quarkus

Quarkus has been designed from the grounds up to be fully compatible with Microprofile specifications. Once that you have a basic JAXRS application, we can start adding the Microprofile extensions we need.

First a basic project creation:

mvn io.quarkus:quarkus-maven-plugin:0.20.0:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=quarkus-demo \
    -DclassName="org.acme.Endpoint" \
    -Dpath="/secured"

Now let’s see how to add the single extensions:

Health:

$ mvn quarkus:add-extension -Dextensions=health

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-smallrye-health</artifactId>
</dependency>

Fault Tolerance:

$ mvn quarkus:add-extension -Dextensions=fault-tolerance

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>io.quarkus:quarkus-smallrye-fault-tolerance</artifactId>
</dependency>

Metrics:

$ mvn quarkus:add-extension -Dextensions=metrics

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>io.quarkus:quarkus-smallrye-metrics</artifactId>
</dependency>

OpenAPI:

$ mvnw quarkus:add-extension -Dextensions="openapi"

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>

OpenTracing:

$ mvn quarkus:add-extension -Dextensions=opentracing

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>io.quarkus:quarkus-smallrye-opentracing</artifactId>
</dependency>

REST Client:

$ mvn quarkus:add-extension -Dextensions=rest-client

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>io.quarkus:quarkus-smallrye-rest-client</artifactId>
</dependency>

JWT:

$ mvn quarkus:add-extension -Dextensions=jwt

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>io.quarkus:quarkus-smallrye-jwt</artifactId>
</dependency>

Configuring MicroProfile with WildFly application server

WildFly application server ships out of the box with support for the latest Microprofile specification. In the current version of WildFly, it includes support for the following extensions:

<extension module="org.wildfly.extension.microprofile.config-smallrye"/>
<extension module="org.wildfly.extension.microprofile.fault-tolerance-smallrye"/>
<extension module="org.wildfly.extension.microprofile.health-smallrye"/>
<extension module="org.wildfly.extension.microprofile.jwt-smallrye"/>
<extension module="org.wildfly.extension.microprofile.metrics-smallrye"/>
<extension module="org.wildfly.extension.microprofile.openapi-smallrye"/>
<extension module="org.wildfly.extension.microprofile.opentracing-smallrye"/>

Not all extensions are available in the “default” WildFly configuration which includes just a subset of Microprofile API by default:

<extension module="org.wildfly.extension.microprofile.config-smallrye"/>
<extension module="org.wildfly.extension.microprofile.health-smallrye"/>
<extension module="org.wildfly.extension.microprofile.jwt-smallrye"/>
<extension module="org.wildfly.extension.microprofile.metrics-smallrye"/>
<extension module="org.wildfly.extension.microprofile.opentracing-smallrye"/>

You can check for standalone-microprofile.xml and standalone-microprofile-ha.xml to have full Microprofile support, or just add the extension by yourself in your configuration:

[standalone@localhost:9990 /] /extension=org.wildfly.extension.microprofile.openapi-smallrye:add()
{"outcome" => "success"}

[standalone@localhost:9990 /] /subsystem=microprofile-openapi-smallrye:add()
{
    "outcome" => "success",
    "response-headers" => {
        "operation-requires-reload" => true,
        "process-state" => "reload-required"
    }
}

In order to buid your Microprofile applications on WildFly it is also recommended to include the following Bill Of Material in your pom.xml:

<dependency>
    <groupId>org.wildfly.bom</groupId>
    <artifactId>wildfly-microprofile</artifactId>
    <version>${wildfly.version}</version>
    <scope>import</scope>
    <type>pom</type>
</dependency>

The following article will provide more details on how to build Microprofile applications on WildFly: How to build Microprofile application on wildfly

If you want to see an example of Microprofile application on WildFly, it is worth starting with this tutorial Configuring Applications with Eclipse MicroProfile Config

Found the article helpful? if so please follow us on Socials