-
Notifications
You must be signed in to change notification settings - Fork 20
02. Getting started
Let's build a Hello World Project with Utterlyidle. First, let's define our directory structure.
mkdir utterlyidle_tutorial
cd utterlyidle_tutorial
mkdir -p src/main/java src/test/java
We'll use Gradle as our build tool but you can also use Maven, Ant/Ivy or any other build tool you like. Just make sure that Utterlyidle and its dependencies are on your classpath. At the time we were writing this tutorial the most recent version of Utterlyidle was 633.
In build.gradle:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
mainClassName = 'HelloUtterlyidle'
sourceCompatibility = 1.6
version = '1.0'
repositories {
mavenCentral()
mavenRepo {
url: "http://repo.bodar.com/"
}
}
dependencies {
compile group: 'com.googlecode.utterlyidle', name: 'utterlyidle',
version: '633' // change to the latest version from https://code.google.com/p/utterlyidle/downloads/list
}
Next, let's create HelloUtterlyidle.java in src/main/java:
import com.googlecode.utterlyidle.annotations.GET;
import com.googlecode.utterlyidle.annotations.Path;
import static com.googlecode.utterlyidle.ApplicationBuilder.application;
public class HelloUtterlyidle {
public static void main(String[] args) throws Exception {
application().addAnnotated(HelloResource.class).start();
}
public static class HelloResource {
@GET
@Path("/")
public String hello() {
return "Hello Utterlyidle";
}
}
}
Execute:
gradle run
When the program starts it prints the following information to the console:
Listening on http://localhost:51367/, started HttpServer in 25.053 msecs
So when you go to http://localhost:51367
in your browser it should print:
Hello Utterlyidle
On your machine the URL will be different as the default server picks a random available port. You can make the port explicit but for the sake of this tutorial we'll stick to the default behaviour.
So what's going on in this code? Main method driven development at its best. We added an annotated resource to our application builder. The resource has a binding between HTTP GET method with a "/hello" path and a Java method that returns a String. After adding a resource, we started the application using a built-in HttpServer.