Getting Started With MicroProfile Config using Payara Micro

Getting Started With MicroProfile Config using Payara Micro

In this tutorial we will learn how to use MicroProfile Config API using Payara Micro
MicroProfile Config API can be used to retrieve configuration information from different sources.

Tools You Will Need
Maven 3.3+
Your favorite IDE. I'm using NetBeans
JDK 1.8+

Generate Project
We will use Payara Micro Maven archetype to generate our project, run the following command.

$ mvn archetype:generate -DarchetypeGroupId=fish.payara.maven.archetypes -DarchetypeArtifactId=payara-micro-maven-archetype -DarchetypeVersion=1.0.1 -DgroupId=fish.payara.micro -DartifactId=microprofile-config-example -Dversion=1.0-SNAPSHOT -Dpackage=com.kodnito.mpconfig -Darchetype.interactive=false

Open pom.xml and add the following :


Here we add MicroProfile Config API dependency to our project.

MicroProfile Config
Inside src/com/resources/META-INF/ create file and add the following :

microprofile.apis={"config", "cdi", "jax-rs", "json-p", "fault tolerance", "healt check", "jwt auth", "metrics", "openapi", "opentracing","rest client"}

Inside src/com/java/com/kodnito/mpconfig/ create file and add the following :

package com.kodnito.mpconfig;


public class ApplicationConfig extends Application {


Now create inside the same package with the following :

package com.kodnito.mpconfig;

import java.util.HashMap;
import java.util.Map;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.inject.ConfigProperty;

public class HelloResource {

    @ConfigProperty(name="username", defaultValue="admin")
    private String username;

    Config config;

    public Response hello() {
        Map<String, Object> configProperties = new HashMap<>();

        configProperties.put("username", username);
        configProperties.put("password", config.getValue("password", String.class));
        configProperties.put("microprofile-apis", config.getValue("microprofile.apis", String[].class));
        return Response.ok(configProperties).build();

@Inject @ConfigProperty annotation is used for injecting a single configuration property and we supply a default value.
@Inject Config here we are injecting the configuration object and we use getValue() method to retrieve the configuration property.

Your final project structure should look like this :

tree .
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── kodnito
        │           └── mpconfig
        │               ├──
        │               └──
        ├── resources
        │   └── META-INF
        │       └──
        └── webapp
            └── index.html

Inside your project directory run the following command to start the Payara Micro server.

$ mvn package payara-micro:start

Now, enter http://localhost:8080/microprofile-config-example-1.0-SNAPSHOT/api/hello in browser's address bar and see the output.

Share this: