If you get tired of "Spring Boot", why not try "jooby"?

In the projects I've been involved with recently, I feel that the adoption of "Spring Boot" (or "Spring Web") has become the de facto standard. I am very grateful for the many features that "Spring Boot" provides, but I am also dissatisfied with it.

For example:

I happened to meet jooby, and I was impressed with it, so I would like to introduce it.


What is Jooby

It is a framework that focuses on assigning paths to "functions that convert to response".

The official website says, "It's a scalable, fast, module-oriented, micro-framework made with java." When I was told about micro frameworks, I thought it was "buzzword-like and smoky." When I tried using it, I thought it was a framework that was "starting to be compact and easy to expand."

Also, when I actually ran the app, it started up quickly and I felt the "micro-framework style". (The netty based server will start)

Official site sample source

Java sample

import org.jooby.Jooby;
public class App extends Jooby {
  { get("/", () -> "Hey Jooby!"); }
  public static void main(final String[] args) {
    run(App::new, args);
  }
}

This alone will start the web application server. It's also nice that ** kotlin is also supported ** as standard.

Kotlin sample

import org.jooby.*
class App : Kooby ({
    get("/") { "Hey Jooby!" }
})
fun main(args:Array<String>) {  run(::App, *args) }

In this blog, I will introduce it in the source of kotlin below.


Routing implementation

The get ("/") {"Hey Jooby! "} In the "Kotlin sample" above is the routing implementation. It means that when you access the root path, the string Hey Jooby! Is returned. (Intuitive and very easy to understand)

    get("/hoge") { "hogeres" }
    post("/foo/boo") { FooControler.boo() }

If you implement such a thing, it means that the web application is completed.

Below is a slightly more complicated example. (When "Handling for requests and responses" and "Using template engine")

get("/boo/hoge") { req , rsp -> BooControler.hoge(req.param("name") , rsp)
    Results.html("freemarker/boo/hoge.html")
}

I think it's nice to be able to intuitively implement both easy and difficult things.


Implementations other than routing

I will introduce implementations that are likely to be used other than routing.

Exception handling

You can implement what to respond to when an exception occurs.

err { req, rsp, err ->
    val cause = err.cause
    when (cause) {
      is MyException1 -> { /*Response implementation*/ }
      is MyException2 -> { /*Response implementation*/ }
      else -> Err.DefHandler().handle(req, rsp, err)
    }
}

If you want to handle only specific exceptions, you can also do the following.

err(MyException1::class.java) { req, rsp, err -> /*abridgement*/}

Filter ###

You can insert filtering before and after processing the request. This allows you to output access logs and add HTTP headers and cookies uniformly or conditionally.

before("**") { req, rsp -> initLoginfo(req, rsp) }

after("**") { req, rsp, result ->
    accessLog(req, rsp, result)
            .header("Access-Control-Allow-Origin", appEnv.allowOrigin)
            .header("Access-Control-Allow-Credentials", true)
}

MODULES

jooby is an idea that extends the function by specifying the "module" to be used. The standard support is described in [MODULES] mod on the official website.

Before routing, set the dependency of Maven and gradle, The basic usage is to implement and add functions such as ʻuse ()`.

Here are some examples.

Freemarker Used for template engine

You can use the Freemarker template by coding as follows.

    use(Ftl("/freemarker"))

In the above example It means that you will go to the template from the directory where the classpath passes to the freemarker directory.

When you actually use the template, you will write the code as follows.

get("/hoge/") {
    Results.html("hoge/index.html")
            .put("hogeParam", hogeBoo.hoge)
}

In the above example, in the template file freemarker / hoge / index.html, you can access the object like the variable$ {hogeParam}. (You can use Freemarker as standard)

Use jackson for RESTish API

If you code as follows

    use(Jackson())

Just write code like get ("/ myjson") {MyJsonObject (firstName = "Yamada", name = "tarou", age = "25")} and the response will be returned as json, RESTish It makes it easier to create APIs.

However, if you just want to return a string, the return value will behave as if it were enclosed in double quotes. (For example, get ("/ alive / chek") {"I'm alive"} will result in the response string being "" I'm alive ")

If you want to return a response other than json, I think it is better to let Jackson form json by yourself instead of using ʻuse (Jackson ()) `.


Other micro web framework

Another popular "micro web framework" is [Spark Framework] sp. The idea of mapping a path to a response is similar to jooby.

In terms of simplicity, it's even easier than jooby. However, jooby seems to be easier for fine handling. Also, in terms of expandability with [MODULES] and mod, I think that jooby will be better off.

Please compare which one is better depending on the situation you use.


jooby How was it?

Good luck


Related page

Recommended Posts

If you get tired of "Spring Boot", why not try "jooby"?
If you get tired of drawing an ER diagram
When @Transactional of Spring Boot does not work
[Spring Boot] If you use Spring Boot, it was convenient to use a lot of util.
If you want to separate Spring Boot + Thymeleaf processing
Try Spring Boot from 0 to 100.
Get started with Spring boot
Try using Spring Boot Security
Try Spring Boot on Mac
Get a proxy instance of the component itself in Spring Boot
Spring Boot 2.0 Actuator, 3 changes you need to know to get it working
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
Going out of message (Spring boot)
Get validation results with Spring Boot
[Spring Boot] Role of each class
Try running Spring Boot on Kubernetes
Spring Boot + MyBatis I get this error if I don't set the database