Baratine on GitHub

Hello World - Eclipse/Maven

This tutorial will walkthrough deploying the Hello World tutorial in the Eclipse IDE. By the end of the tutorial, you will have deployed your first service on the Baratine Web Server.

Quick Glance

Create New Maven Project

Open Eclipse and navigate to the following:

File > New > Project > Maven Project

../../_images/archetype.png

Under the “Select an Archetype” screen, select the maven-archetype-quickstart Artifact Id and select Next.

Supply a Group Id and Artifact Id and select Finish.

Edit your .pom file, add Baratine

The Baratine API is provided through the following Maven dependency:

<dependency>
        <groupId>io.baratine</groupId>
        <artifactId>baratine</artifactId>
        <version>1.0.0</version>
</dependency>

Create “Hello World” Main

Select your generated project and create a new class file within the package. Name the file “Hello.java”.

This hello, world shows JSON/REST and Baratine’s persistence where the greeting can be changed.

The code for our entire 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
package tutorial;

import io.baratine.service.*;
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.

Create index.html

In a standard Maven build, the index.html belongs in src/main/resources/public/index.html.

Because our example is a REST example, the index.html contains JavaScript to query the server for its current greeting.

 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
35
36
37
38
39
40
<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>

Start Server & Deploy

In Eclipse, select your Hello World Project and choose Run > Run as > Java Application from the top menu

You will see a print out in the bottom console, displaying information about the Baratine server that is running. Of note to us, is the *:8080 port Baratine is listening to

../../_images/server.png

From here, open your browser and navigate to localhost:8080/hello

Provide value for the edit box and hit <Enter>.

../../_images/browser-hello-maven.png

Congratulations, you’ve now deployed your first Baratine Service! Because your service is running on the Baratine Web Server it automatically received the following:

  1. High Performance
  2. Reactive - Baratine is asynchronous which means no blocking requests & efficient resource consumption

Next Steps

  • Download Baratine or add a maven dependency to start
  • See Web for HTTP/Web documentation.
  • See Vault Services for persistent Vault documentation.