In this first post we will cover a lot of our favorite tools - Gradle, Jetty, JRebel and IntelliJ Idea. We are going to build a very simple RESTFul API using the awesome RESTEasy framework and show how to shorten the build/deploy/test cycle with JRebel. The tutorial is split in three parts.

The first part will explain how to create a RESTful blogging service with Gradle and RESTEasy in literally 3 minutes.


Start from scratch

Let's start with creating the project's folders structure and the Gradle build file. Gradle is the most awesome build tool for JVM based languages. If you don't know Gradle, read about it and make it your tool of choice for building code (yes, Gradle kicks Maven ass big time).

Gradle doesn't support Maven-like archetypes (yet) and this is probably the only manual tedious task in the whole setup.

Create a folder structure similar to the one in the picture below.

You have probably noticed that the folder structure is identical to the Maven web-app artifact.

Now, let's create the Gradle build file, build.gradle:

apply plugin: 'java'
apply plugin: 'jetty'
apply plugin: 'idea'

version = '1.0-SNAPSHOT'
group = 'com.aestasit.restdemo'
jettyRun.contextPath = '/rest-blog'
repositories {
  mavenRepo urls: ""
  mavenRepo urls: ""

dependencies {
  compile 'org.jboss.resteasy:resteasy-jaxrs:2.0.1.GA'
  compile 'org.jboss.resteasy:resteasy-jettison-provider:2.0.1.GA'
  compile 'javax.servlet:servlet-api:2.5'

Place the file in the root of the project. At this point, you can build the project running the following Gradle task in the command line:

gradle build

There is not much to build yet, so objectively the outcome of this step is not very interesting. You'll notice that Gradle will fetch the RESTEasy libraries and their dependencies for you (this is going to take some time).

Let's add some code, using our favourite IDE, Intellij IDEA. But first, we need to generate the IDEA project files. Couldn't be easier. Go back to the command prompt and type:

gradle idea

Three IDEA project files will appear in the root directory. Now fire IDEA and create a new Java project ( File > Create project from scratch ). Name the project whatever you like and remember to check "Do not create source directory". The project should be located outside the Gradle project folder.

Once the project is created, add the a new module ( File > New module > Import existing module ) and select the iml file you have generated with Gradle. Now you should have your Rest project properly configured in Intellij IDEA. If you open the Project structure (F4) and select the "Dependencies" tab, you'll see that all the project dependencies (RESTEasy etc.) are set. You can start coding straight away.

Where's the code?

We will create three classes exposing some methods for a simple blogging service.

The ubiquitous POJOs:

@XmlRootElement(name = "post")
public class BlogPost implements Serializable {

  private long id;
  private String title;
  private String context;
  private Date postDate;
  private String author;

  public BlogPost() {} // Default Constructor, don't remove

  // ... add getters/setters and constructor ...

The Service interface:


import java.util.List;

public interface BlogService {

  BlogPost getPostById(String id);
  List getTags();


The Service implementation:

import java.util.Date;
import java.util.List;

public class RestBlogService implements BlogService {

  public BlogPost getPostById(@PathParam("id") String id) {
    // Database lookup and return the Blog post by ID
    // the meanwhile, just for testing
    BlogPost blogpost = new BlogPost(Long.parseLong(id), "My new car", "I bought a car", new Date(), "me");
    return blogpost;

  public List getTags() {
    return null;


You have noticed that the Service implementation has some annotations. These are JAX-RS annotations that, along with the classes and interfaces provided by JAX-RS API, allow you to expose simple POJOs as web resources. I'm not going into too much details here about the JAX-RS API. I recommend you read the excellent RESTEasy documentation and, if you are completely new to RESTFul Services, take a look at TODO.

The first annotation @Path("blog") is the URI to the resource you want to @GET (REST supports GET, POST, DELETE and UPDATE HTTP protocol's methods). The annotation @Produces is behind the cardinal concept of Content Negotiation. With Content Negotiation a resource may have multiple representations in different formats, so a client can receive the best representation for its abilities. In our example, we are producing a JSON formatted response.

But wait, there is no reference to JSON in our code. Take a closer look to the POJO class. It contains a JAXB annotation @XmlRootElement. JAXB is "the" JAVA-XML binding framework. It's so good, that it became part of the Java 1.6 platform. Why JAXB? From Wikipedia:

JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects.

JAXB transforms objects into XML but not JSON. Again, take a closer look at the build file. One of the dependencies is org.jboss.resteasy:resteasy-jettison-provider:2.0.1.GA.

Jettison is a Codehaus project that serializes objects to/from JSON. JAXB can be "decorated" with different providers allowing the output to be formatted using different protocols. One of these providers is Jettison. From a development point of view, there is not much to do in order to return JSON: annotate your class using the JAXB annotations, drop into your classpath a JAXB provider and specify your content format in your Rest service. You can read more about RESTEasy and JAXB providers here.

We are almost ready to test our little Rest service. Two more steps before we hit the red button.

Add this web.xml file to the WEB-INF folder:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""

Add this class in one of your packages:

public class ApplicationBootstrap extends Application {
    Set singletons = new HashSet();

    public ApplicationBootstrap() {
        singletons.add(new BlogServiceRestImpl());

    public Set&gt; getClasses() {
        HashSet&gt; set = new HashSet&gt;();
        return set;

    public Set getSingletons() {
        return singletons;

The ApplicationBootstrap class is needed to actually initialize your Restful service. There are several ways to initialize a RESTEasy application. In a real-world system, I would probably use the super-easy Spring integration.

We can finally test our application. Go back to your console and type:

gradle clean jettyRun

If your code doesn't contains errors and all the plumbing is connecting the right dots, you should be able to fire your browser to http://localhost:8080/rest-blog/blog/post/10 and display the JSON response:

   "context":"I bought a car",
   "title":"My new car"

Cool, uh? In the next part of this tutorial, we are going to introduce JRebel and how JRebel can help to achieve Ruby On Rails-like productivity.

You can find the code for this tutorial in the Aestas Git Repo.

Subscribe to our newsletter!

Be the first to know when we release a new tool! We will never SPAM or sell your e-mail to third party! Not unless we are being tortured with a blow torch and a pair of plaiers.