First things first: Kotlin first-class support

What’s new in Jooby 1.1?

Edgar Espina

Jooby 1.1 is here. Kotlin support is definitely the most exciting feature in this release but it’s not the only reason why you should take a closer look at this micro web framework for Java.

We recently announced a new stable release for Jooby. This new release comes with a couple of new and exciting features.

In this post, we are going to talk about some of these features.

Kotlin first-class support

Kotlin support is definitely the most exciting feature in 1.1. It’s well known that Java libraries just works in Kotlin, so library/framework providers don’t need to do anything special.

For example here is how it looks a Jooby application in Kotlin:

package starter.kotlin

import org.jooby.*

class App: Jooby() {
  init {
    get("/", {req->
      "Hello " + req.param("name").value("Kotlin")
    });
  }
}

fun main(args: Array<String>) {
  Jooby.run(::App, args);
}

Not bad, but there isn’t anything special here we just make one–to-one translation from Java to Kotlin.

kotlin idioms

Because we love Kotlin, we do provide a Kotlin module which allows us to write idiomatic Kotlin:

import org.jooby.*

fun main(args: Array<String>) {
  run(*args) {
    get {
      val name = param("name").value("World")
      return "Hello $name!"
    }
  }
}

Much better, ugh?

The run function is type-safe builder which give you implicit access to a Jooby instance. Same for get function but this time gives you implicit access to the Request object.

Here is another idiom:

package starter.kotlin

import org.jooby.*

fun main(args: Array<String>) {
  run(*args) {
    route("/api/pets") {
      get({->
        // list pets
      })
      get("/:id", {req ->
        // pet by ID
        val id = req.param("id").intValue
      })
      post({req ->
        // list pets
        val pet = Petreq.body(Pet::class);
      })
    }
  }
}

The route idiom group one or more route under a common path, like:/api/pets

LiveReload

No more browser refresh button!!

 

</span>
<pre>import org.jooby.*;
import org.jooby.json.*;
import org.jooby.livereload.*;

...
  {
     use(new Jackson());
     use(new LiveReload());
     ...
  }

The LiveReload module listens for file changes and automatically reload the browser.

rocker templates

Rocker is a fast, safe and statically typed template engine:

// views/main.rocker.html

@args (String title, RockerBody content)

<html>
    <head>
        <title>@title</title>
    </head>
    <body>
    @content
    </body>
</html>

// views/index.rocker.html
@args (String message)

@views.main.template("Home") -> {
    
<h1>Hello @message!</h1>

}

You can efficiently render them from your application:

import org.jooby.rocker.*;

{
  use(Rockerby());

  get("/", () -> views.index.template("Rocker"));
}

requery db

Requery is a modern SQL, statically typed persistence engine:

import org.jooby.jdbc.*;
import org.jooby.requery.*;

...
{
  use(new Jdbc());

  use(new Requery(Models.DEFAULT));

  get("/people", () -> {

    EntityStore store = require(EntityStore.class);
    return store.select(Person.class)
       .where(Person.ID.eq(req.param("id").intValue()))
       .get()
       .first();
  });

}

there is more

Hope you enjoy it!

Complete release notes are available here: https://github.com/jooby-project/jooby/releases/tag/v1.1.0

asap

This post was originally published on Medium

Author

Edgar Espina

Edgar is the creator of Jooby. He is also an Open Source enthusiast.

Find him on Twitter: @edgarespina


Comments
comments powered by Disqus