RESTEasy, embedded Jetty and Fat JARs

In the previous post, I wrote about the steps I followed when getting started with the RESTEasy framework.

In this post, I'll walk you through the steps I followed when embedding Jetty and packaging a RESTful API as a fat JAR (a fat JAR or uber-JAR is a JAR file that contains all of a project's class files and resources packaged together with all of it's dependencies).


  • OpenJDK for Java 1.8
  • Git
  • Maven (I'm using 3.3.9)
  • The Eclipse IDE for Java EE Developers (I'm using Neon)

Note: This post will walk you through the steps required to install the OpenJDK. And, this post will walk you through the steps required to install Git, Maven and the Eclipse IDE.

Create a scaffold

Let's use Maven's quickstart archetype to create the scaffolding for our project:

mvn archetype:generate \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DarchetypeVersion=1.1 \
  -DgroupId=org.robferguson.resteasy.examples.fatjar \
  -DartifactId=fatjar \
  -DarchetypeRepository=local \

The Eclispe IDE

Follow the steps in this post to import the template into the Eclispe IDE.

Creating a Resource

We can reuse the MessageResource class from the previous post (we just need to refactor the package name):


public class MessageResource {

  public Response printMessage(@PathParam("param") String msg) {
    String result = "Hello " + msg + "!";
    return Response.status(200).entity(result).build();

Registering a Resource

We can reuse the HelloWorldApplication class from the previous post (we just need to refactor the package name, the class name and the constructor name):


public class FatJarApplication extends Application {

  public FatJarApplication() {}

  public Set<Object> getSingletons() {
    HashSet<Object> set = new HashSet<Object>();
    set.add(new MessageResource());
    return set;

Note: I hope you noticed that we're not using the @ApplicationPath annotation, that's because we are going to set the application path ("/api") using an initialisation parameter in our Main class (see below).

Embedding Jetty

Jetty has a slogan, "Don’t deploy your application in Jetty, deploy Jetty in your application!". What this means is that Jetty can be instantiated and used in a Java program just like any plain old java object.

To embed Jetty we need to create a server instance, configure the server and then start the server. We also need to provide an entry point (i.e., a main method) for our application:

package org.robferguson.resteasy.examples.fatjar;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;

public class Main {

  static final String APPLICATION_PATH = "/api";
  static final String CONTEXT_ROOT = "/";

  public Main() {}

  public static void main( String[] args ) throws Exception
      new Main().run();
    catch (Throwable t)

  public void run() throws Exception
    final int port = 8080;
    final Server server = new Server(port);

    // Setup the basic Application "context" at "/".
    // This is also known as the handler tree (in Jetty speak).
    final ServletContextHandler context = new ServletContextHandler(
        server, CONTEXT_ROOT);

    // Setup RESTEasy's HttpServletDispatcher at "/api/*".
    final ServletHolder restEasyServlet = new ServletHolder(
        new HttpServletDispatcher());
    context.addServlet(restEasyServlet, APPLICATION_PATH + "/*");

    // Setup the DefaultServlet at "/".
    final ServletHolder defaultServlet = new ServletHolder(
        new DefaultServlet());
    context.addServlet(defaultServlet, CONTEXT_ROOT);


We're configuring Jetty in our code so we don't require a WEB-INF/web.xml.

Here's how the project should look in Eclipse's Project Explorer:


Let’s take a look at the Fat JAR example's pom.xml:


The first few elements — the groupId, artifactId, version and packaging — are known as the Maven coordinates, and they uniquely identify a project. The packaging element tells Maven that this project will build a JAR file.


Maven properties are value placeholders, their values are accessible anywhere within a POM using the following notation: ${X} (e.g., ${jetty.version}).


  <!-- RESTEasy runtime dependencies -->

  <!-- Jetty runtime dependencies -->



The dependencies element lists all the library dependencies the project needs to compile and run. The Fat JAR example is dependent on RESTEasy (the core library and the ServletContainerInitializer integration library), the Java Servlet API library and Jetty's servlet library.

The build element contains configuration information that defines how Maven should build the project:


The first item is the finalName element, which is used to name the project's JAR file.

Next we have the plugins element, this section contains information about the Maven plugins that will be used to build the project:



                <transformer implementation=
                <transformer implementation=



The maven-shade plugin is responsible for packaging our application as a fat JAR (and where required to shade (i.e., rename) it's dependencies).

Build and run the example program

The best way to learn about the RESTEasy framework is to build and run example programs. Lets start by cloning my RESTEasy examples repository:

git clone

Then we can build the 'Fat JAR' example (in the examples/fatjar directory) and run some tests:

mvn clean install

To run the example:

java -jar target/fatjar-1.0-SNAPSHOT.jar

You should see output like:

2016-12-06 15:24:08.011:INFO::main: Logging initialized @219ms
2016-12-06 15:24:08.157:INFO:oejs.Server:main: jetty-9.3.z-SNAPSHOT
Dec 06, 2016 3:24:08 PM org.jboss.resteasy.spi.ResteasyDeployment
INFO: RESTEASY002225: Deploying class
Dec 06, 2016 3:24:08 PM org.jboss.resteasy.spi.ResteasyDeployment
INFO: RESTEASY002220: Adding singleton resource
    from Application class
2016-12-06 15:24:08.726:INFO:oejsh.ContextHandler:main: Started
2016-12-06 15:24:08.776:INFO:oejs.AbstractConnector:main: Started
2016-12-06 15:24:08.784:INFO:oejs.Server:main: Started @994ms

In your browser navigate to:


You should see output like:

Source Code: