IMPORTANT: The Thorntail project (formerly known as WildFly Swarm) has reached End Of Life. The last Thorntail release is the 2.7.0. You are recommended to evaluate a migration plan for your applications. Check this tutorial to learn more: How to migrate Thorntail applications
In this hands-on tutorial we will demonstrate how to publish quickly a REST Service using WildFly Swarm.
First of all, we will take a Simple REST Service taken from my github repository:
package com.itbuzzpress.chapter11.service;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import com.itbuzzpress.chapter11.model.SimpleProperty;
@Path("/simple")
public class SimpleRESTService {
@GET
@Path("/text")
public String getHello ()
{
return "hello world!";
}
@GET
@Path("/json")
@Produces(MediaType.APPLICATION_JSON)
public SimpleProperty getPropertyJSON ()
{
SimpleProperty p = new SimpleProperty("key","value");
return p;
}
@GET
@Path("/xml")
@Produces(MediaType.APPLICATION_XML)
public SimpleProperty getPropertyXML ()
{
SimpleProperty p = new SimpleProperty("key","value");
return p;
}
}
The REST Service does nothing particularly fancy, however it can be used as a simple playground for REST Services producing content in different formats. An Activator class is required to set the REST ApplicationPath:
package com.itbuzzpress.chapter11.activator;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
}
Finally, the SimpleProperty class is nothing more than a JavaBean:
package com.itbuzzpress.chapter11.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class SimpleProperty {
public SimpleProperty() {
}
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public SimpleProperty(String key, String value) {
super();
this.key = key;
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
So far nothing new. In order to build our Micro Service, we will include the wildfly-swarm plugin in the pom.xml file and the required dependencies;in our case it’s just jaxrs which will automatically trigger the sub-dependencies like Undertow web server.
<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.example</groupId>
<artifactId>demo</artifactId>
<name>Wildfly Swarm Example</name>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<version.wildfly.swarm>1.0.0.Final</version.wildfly.swarm>
<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>org.wildfly.swarm</groupId>
<artifactId>bom</artifactId>
<version>${version.wildfly.swarm}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly.swarm}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Java EE 7 dependency -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- Wildfly Swarm Fractions -->
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
</dependencies>
</project>
So with just a single dependency (jaxrs), and the javaee-api, you will build-up an application which contains, behind the hoods, a custom application server.
Building the application
You can build your application with the maven’s package goal:
mvn clean package
Here is what was created in the target folder of your project:
[francesco@localhost javaee7-rest-basic]$ ls target . . . . . javaee7-rest-swarm-1.0.0-swarm.jar javaee7-rest-swarm-1.0.0.war
Running the application
You can run your application using Maven:
mvn wildfly-swarm:run
Or simply with java -jar:
java -jar ./target/javaee7-rest-swarm-1.0.0-swarm.jar
You will be able to invoke the REST Service using the URL: http://localhost:8080/rest/simple/[Path]
For example:

By using WildFly Swarm you can easily provision Micro Services using just the API you need in your application!