09 Feb 2015, 00:00

Hello world using Ratpack.

About Ratpack

Ratpack is a set of Java libraries that facilitate fast, efficient, evolvable and well tested HTTP applications built on Netty event driven networking engine,

After crawling into its source code, I find it simple and easy to use and it feels right, why? because:

  1. It’s super simple, its core has very limited number of dependencies
  2. Has out of the box extensions such as Guice, Jackson, etc, but nothing dictates you using them thanks to Guice modular architecture.
  3. Compatible with Java8 lambda syntax
  4. Testing is very easy.

Code can be written in Groovy or Java, and since java8 lambda syntax is so pretty, I find it suitable for the Ratpack handlers architecture quit well.

So here’s a simple hello world using Ratpack and Java8,

The build file (Gradle)

While used to Maven, It feels like Ratpack folks like Gradle, so I said why not use it?

Here’s a simple build.gradle file:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath "io.ratpack:ratpack-gradle:0.9.13"
  }
}

apply plugin: "io.ratpack.ratpack-java"
apply plugin: "idea"

dependencies {
  //Ratpack uses slf4j for logging
  runtime "org.slf4j:slf4j-simple:1.7.5"
}

//we need this since 0.9.13 is based on snapshot version of Netty
repositories {
  jcenter()
    maven {
      url "http://clinker.netty.io/nexus/content/repositories/snapshots"
    }
}

Create basic directories

mkdir -p src/main/java
mkdir -p src/ratpack

Run in the command like gradle idea to create Intellij project:

gradle idea

One of the things I mostly like about Ratpack is that it has no real notion of a “container”, you can simply start it a plain main method just like that:

import ratpack.server.RatpackServer;
import ratpack.server.ServerConfig;

/**
 */
public class Server {
    public static void main(String[] args) throws Exception {
        RatpackServer server = RatpackServer.of(b -> b
                        .serverConfig(ServerConfig.embedded()) //default config
                        .registryOf(r -> r.add(String.class, "world"))  // registry of supporting objects - optional
                        .handlers(chain -> chain                    // request handlers - required
                                        .get("hello", ctx -> ctx.render(ctx.get(String.class) + " !"))
                        )
        );

        server.start();
    }
}

Server will start with on some available port and will print: Ratpack started for http://localhost:65478

now hit in the browser http://localhost:65478/hello ! :)