A practical use case for micro services is to provide access to resources using REST Api. As an example, we will show how to set up a JAX-RS server using Thorntail and adding Swagger UI on the top of it to easily document and consume the available services.

As usual, start by using the Thorntail project generator to create the basic structure of a project: https://thorntail.io/generator/

Within the Thorntail project generator, select the JAX-RS and Swagger dependencies fractions as you can see from the following picture:

thorntail rest example tutorial

The Swagger fraction will generate the swagger.json descriptor for all JAX-RS resources found. The Swagger WebApp will provide an UI for displaying and invoking the services discovered by Swagger. Click "Generate Project" to download the project and open it from your favorite IDE.

Project configuration

As you can see from the pom.xml, the following dependencies have been automatically added so that you can bootstrap a WildFly server with JAX-RS and Swagger fractions:

<?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">
  <name>Thorntail Example</name>





Adding REST Services

You can add some REST resources to test your project, such as this simple HelloWorldEndpoint taken from Thorntail examples:

package com.example.demomicroservice.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Date;

@Api(value = "/time", description = "Get the time", tags = "time")
public class HelloWorldEndpoint {

    @ApiOperation(value = "Get the current time",
            notes = "Returns the time as a string",
            response = String.class
    public String get() {
        return String.format("{\"value\" : \"The time is %s\"}", new Date());

As you can see from the above example, the most interesting annotation is @Api which marks a class as a Swagger resource and provides some description that will be included in the Web UI. The project should also include a class extending javax.ws.rs.core.Application to define the rest @ApplicationPath:

package com.example.demomicroservice.rest;

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

public class RestApplication extends Application {


Finally, we will include a class implementing the ContainerResponseFilter interface, which works like a filter for the ContainerResponse extension point on the server side, in order to filter the response message after the invocation has executed:

package com.example.demomicroservice.rest;

import java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;

public class CORSFilter implements ContainerResponseFilter {

    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
        responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        responseContext.getHeaders().add("Access-Control-Max-Age", "-1");
        responseContext.getHeaders().add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");


Running the Thorntail application

Build you application the usual way:

$ mvn clean install

Then you can run it as:

java -jar target/demo-thorntail.jar

To GET the JAX-RS resource simply issue:

$ curl http://localhost:8080/time/now

Then, by pointing your browser at http://localhost:8080/swagger.json you should see the API documented similar to this.


    "swagger": "2.0",
    "info": { },
    "basePath": "/",
    "tags": [
            "name": "time"
    "paths": {
        "/time/now": {
            "get": {
                "tags": [
                "summary": "Get the current time",
                "description": "Returns the time as a string",
                "operationId": "get",
                "produces": [
                "parameters": [ ],
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "schema": {
                            "type": "string"


Now reach the Swagger UI at: http://localhost:8080/swagger-ui and provide as input the swagger.json:

thorntail rest example tutorial

As you can see, you have access to REST Services as describer by the swagger.json. By clicking on them, you can test them as well from the Swagger UI!

Enjoy Thorntail and REST Services!