MicroProfile Config using TomEE

Posted by Hayri Cicek on September 09, 2018

TomEE version 7.1.0 now supports MicroProfile and in this quick tutorial I will show you how to get started with MicroProfile Config using TomEE application server.

Tools You Will Need
Maven 3.3+
Your favorite IDE
JDK 1.8+

Clone the TomEE Starter Project from Github

$ git clone https://github.com/cicekhayri/tomee-javaee-rest-starter-project.git

Now it's time to add some dependencies.
Open the project in your favorite IDE and add the following to the pom.xml


We added Geronimo Config dependency and your pom.xml should look like this now :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">




In project directory run the following command to download the dependencies.

$ mvn install

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


Open HelloWorldEndpoint.java and update the file to look like this :

package com.kodnito.restapi.rest;

import java.util.HashMap;
import java.util.Map;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.inject.ConfigProperty;

public class HelloWorldEndpoint {

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

    Config config;

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

        configProperties.put("username", username);
        configProperties.put("password", config.getValue("password", String.class));
        configProperties.put("application.server", config.getValue("application.server", 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
    │   │           └── restapi
    │   │               └── rest
    │   │                   ├── ApplicationConfig.java
    │   │                   └── HelloWorldEndpoint.java
    │   ├── resources
    │   │   └── META-INF
    │   │       └── microprofile-config.properties
    │   └── webapp
    │       └── WEB-INF
    │           └── beans.xml
    └── test
        └── java
Inside your project directory run the following command to start the TomEE server.

$ mvn clean package tomee:run

Now, enter http://localhost:8080/restapi/api/hello in browser's address bar and see the output.

Clone the final project from GitHub

$ git clone https://github.com/cicekhayri/tomee-microprofile-config.git

Share this: