MicroProfile Config using TomEE

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+

Git



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

< dependency > < groupId > org.apache.geronimo.config </ groupId > < artifactId > geronimo-config-impl </ artifactId > < version > 1.0 </ version > </ dependency >



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 " > < modelVersion > 4.0.0 </ modelVersion > < groupId > com.kodnito </ groupId > < artifactId > restapi </ artifactId > < version > 1.0-SNAPSHOT </ version > < packaging > war </ packaging > < properties > < tomee.version > 7.1.0 </ tomee.version > < javaee.api > 7.0 </ javaee.api > < project.build.sourceEncoding > UTF-8 </ project.build.sourceEncoding > < failOnMissingWebXml > false </ failOnMissingWebXml > < maven.compiler.target > 1.8 </ maven.compiler.target > < maven.compiler.source > 1.8 </ maven.compiler.source > </ properties > < dependencies > < dependency > < groupId > javax </ groupId > < artifactId > javaee-api </ artifactId > < version > ${javaee.api} </ version > < scope > provided </ scope > </ dependency > < dependency > < groupId > org.apache.geronimo.config </ groupId > < artifactId > geronimo-config-impl </ artifactId > < version > 1.0 </ version > </ dependency > </ dependencies > < build > < finalName > ${project.artifactId} </ finalName > < plugins > < plugin > < groupId > org.apache.tomee.maven </ groupId > < artifactId > tomee-maven-plugin </ artifactId > < version > ${tomee.version} </ version > < configuration > < tomeeVersion > ${tomee.version} </ tomeeVersion > < tomeeClassifier > plus </ tomeeClassifier > </ configuration > </ plugin > </ plugins > </ build > </ project >



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 :





application.server=TomEE username=root password=pass



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 ; @RequestScoped @Path ( "hello" ) public class HelloWorldEndpoint { @Inject @ConfigProperty ( name = "username" , defaultValue = "admin" ) private String username ; @Inject Config config ; @GET @Produces ( MediaType . APPLICATION_JSON ) 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: