Baratine on GitHub

Hello World REST - Gradle

This tutorial will walk you through deploying Baratine with Gradle. Gradle is a build and automation tool that can automate building, testing, and deploying tasks. At the end of this tutorial you will have a standalone exectuable jar that will deploy your Hello World Baratine service.

The Hello World has the following features:

  • Uses JSON REST as its primary API.
  • Updates the greeting with a HTTP POST.
  • Is persistent. Data is stored in Baratine’s internal document database.
  • Is thread safe without requiring additional locks.

Directory Structure

You’ll create three files:
  • build.gradle is the gradle build file
  • src/main/java/hello/Hello.java is the Java source
  • src/main/resources/public/index.html is the main page
hello/
  build.gradle
  src/main/
       java/
         hello/Hello.java
       resources/
         public/index.html

build.gradle

build.gradle is the Gradle build file, which includes any dependencies like Baratine itself.

In the gradle build below, we’ve included a Baratine plugin, which adds a boot-jar task to create an executable boot jar.

apply plugin: 'maven'
apply plugin: 'java'
apply plugin: 'application'

repositories {
  mavenCentral();
}

dependencies {
  compile 'io.baratine:baratine:1.0.0'
}

mainClassName='hello.Hello'

To build the application use:

$ gradle build

which will create a build/libs/hello.jar.

To run the application directly use:

$ gradle run

Create the Hello Java Source

Java source is in src/main/java. The code for Hello World is as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package hello;

import io.baratine.service.*;
import io.baratine.vault.*;
import io.baratine.web.*;

@Asset
@Service
public class Hello
{
  private String greeting = "Hello, world";

  @Get
  public void hello(Result<Hello> result)
  {
    result.ok(this);
  }

  @Post
  @Modify
  public void update(@Body Hello hello, Result<String> result)
  {
    this.greeting = hello.greeting;

    result.ok("ok");
  }

  public static void main(String []args)
  {
    Web.include(Hello.class);

    Web.go(args);
  }
}

Line notes:

    1. @Asset marks the service as persistent, saved to the internal database.
    1. @Service is a non-locking, asynchronous component.
    1. Field updates are thread-safe because Hello is a Baratine service.
    1. @Get marks a HTTP GET method. The URL is “/hello” from the method name.
  • 8a. Results are returned continuation-style because Baratine is async; methods must not block waiting for results.
  • 8b. Output is returned as JSON.
  • 10. A more complete example would return a transfer object or a copy of Hello instead of the service itself.
    1. @Post marks a HTTP POST.
    1. @Modify marks the service as needing a save because the data changes.
    1. @Body parses the POST content into a Hello object.
    1. Web.include adds the HTTP routes in Hello to the web server.
    1. Web.go starts the web server with the given arguments.

Add a index.html

index.html belongs in src/main/resources/public/index.html

index.html:

<script type="text/javascript">

function hello()
{
  var http = new XMLHttpRequest();

  http.onreadystatechange = function() {
    if (http.readyState == 4 && http.status == 200) {
      var json = JSON.parse(http.responseText);

      document.getElementById("data").innerHTML = json.greeting;
    }
  };

  http.open("GET", "/hello", true);
  http.send();
}

function myUpdate()
{
  var greeting = document.update.greeting.value;
  var data = {"greeting" : greeting };

  var http = new XMLHttpRequest();

  http.open("POST", "/update");
  http.setRequestHeader("Content-type", "application/json");
  http.send(JSON.stringify(data));
}

hello();

</script>

Greeting: <span id="data"></span>
<br/>

<form name="update" onsubmit="myUpdate()">
  Update: <input name="greeting" />
</form>

Run with Gradle

You can run directly with the run task:

$ gradle run

Browse http://localhost:8080/.

Build an executable boot jar

The build.gradle can also build a boot jar that contains all the dependencies needed to run the application. The boot jar requires the baratine gradle plugin, which is included in baratine.jar

build.gradle:

apply plugin: 'maven'

buildscript {
  repositories {
    mavenCentral();
  }

  dependencies {
    classpath 'io.baratine:baratine:1.0.0'
  }
}

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'baratine'

repositories {
  mavenCentral();
}

dependencies {
  compile 'io.baratine:baratine:1.0.0'
}

mainClassName='hello.Hello'

Now the following build will also create a build/libs/hello-boot.jar.

$ gradle build

The hello-boot.jar is an executable jar that includes all the project’s dependencies.

You can run the boot jar as follows:

$ java -jar build/libs/hello-boot.jar

Next Steps

  • Download Baratine or add a maven dependency to start
  • See Web for HTTP/Web documentation.
  • See introduction for an introduction.