Getting Started With MicroProfile

by Hayri Cicek


Posted on 29-Jul-2018

In this tutorial I'll show you how to get started with MicroProfile.

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

Generate Maven Project
In your terminal type the following

$ mvn archetype:generate -DgroupId=com.kodnito -DartifactId=getting-started-with-javaee 
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command will generate a new maven project.
Now open the project in your favorite IDE.

Adding Dependencies

Open the pom.xml and change the file to look like this


<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>getting-started-with-microprofile</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>getting-started-with-microprofile</name>
    <packaging>war</packaging>
    
    <properties>
        <version.thorntail>2.0.0.Final</version.thorntail>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.thorntail</groupId>
                <artifactId>bom-all</artifactId>
                <version>${version.thorntail}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>io.thorntail</groupId>
            <artifactId>microprofile</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
   <build>
        <finalName>getting-started-with-microprofile</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>

</project>

We added MicroProfile dependency and the Thorntail (wildfly-swarm.io) Maven plugin.

Rest Controller

The maven command generated two files App.java and AppTest.java which we don't need so the first step is to the delete them.



Create a new class inside com.kodnito package as follows



RestApplication.java

package com.kodnito;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;


@ApplicationPath("/api")
public class RestApplication extends Application {
        
}

@ApplicationPath annotation identifies the application path that will serve as the base for all our endpoints.

Now create a new class called HelloResource inside com.kodnito.resources package as follows



HelloResource.java

package com.kodnito.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("hello")
public class HelloResource {
    
    @GET
    public String hello() {
        return "Hello MicroProfile";
    }
}


@Path annotation identifies the URI path.
@GET annotation indicates that hello() method responds to HTTP GET requests.

Now it's time to test our new rest endpoint
Open your terminal and go to the project directory.
Run the following command

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


Share this: