, ,

Javalin 1.zero.zero accurate is ready! – Javalin: Easy REST APIs for Java and Kotlin

Javalin 1.zero.zero accurate is ready! – Javalin: Easy REST APIs for Java and Kotlin

news picture

Introducing Javalin

Javalin is a truly lightweight internet framework for Kotlin and Java, inspired by Sparkjava and koa.js.
Javalin is written in Kotlin with just a few purposeful interfaces written in Java. This turned into once mandatory to
provide an satisfying and reach identical experience for both Kotlin and Java builders.

Javalin is really more library than framework; you don’t must prolong or implement one thing else
and there are only just a few “Javalin-ideas” you’ve to learn. Let’s watch at some examples:

Hello World

stress-free major(args: Array<String>) {
    val app = Javalin.start up(7000)
    app.secure("/") { ctx -> ctx.consequence("Hello World") }
}
public static void major(String[] args) {
    Javalin app = Javalin.start up(7000);
    app.secure("/", ctx -> ctx.consequence("Hello World"));
}

API structure and server config

val app = Javalin.build().snarl {
    enableStaticFiles("/public")
    enableStandardRequestLogging()
    port(port)
}.start up()

app.routes {
    route("customers") {
        secure(UserController::getAllUserIds)
        submit(UserController::createUser)
        route(":person-id") {
            secure(UserController::getUser)
            patch(UserController::updateUser)
            delete(UserController::deleteUser)
        }
    }
}
Javalin app = Javalin.build()
    .enableStaticFiles("/public")
    .enableStandardRequestLogging()
    .port(port)
    .start up();

app.routes(() -> {
    route("customers"(() -> {
        secure(UserController::getAllUserIds);
        submit(UserController::createUser);
        route(":person-id"(() -> {
            secure(UserController::getUser);
            patch(UserController::updateUser);
            delete(UserController::deleteUser);
        });
    });
});

Filters and mappers

app.sooner than("/some-route/*") { ctx ->  ... } // runs sooner than requests to /some-route/*
app.sooner than { ctx -> ... } // runs sooner than all requests
app.after { ctx -> ... } // runs in any case requests
app.exception(Exception.class) { e, ctx -> ... } // runs if uncaught Exception
app.error(404) { ctx -> ... } // runs if region is 404 (in any case other handlers)
app.sooner than("/some-route/*", ctx -> { ... }); // runs sooner than requests to /some-route/*
app.sooner than(ctx -> { ... }); // runs sooner than all requests
app.after(ctx -> { ... }); // runs in any case requests
app.exception(Exception.class, (e, ctx) -> { ... }); // runs if uncaught Exception
app.error(404, ctx -> { ... }); // runs if region is 404 (in any case other handlers)

WebSockets

app.ws("/websocket") { ws ->
    ws.onConnect { session -> println("Connected") }
    ws.onMessage { session, message ->
        println("Bought: " + message)
        session.far-off.sendString("Echo: " + message)
    }
    ws.onClose { session, statusCode, reason -> println("Closed") }
    ws.onError { session, throwable -> println("Errored") }
}
app.ws("/websocket", ws -> {
    ws.onConnect(session -> Gadget.out.println("Connected"));
    ws.onMessage((session, message) -> {
        Gadget.out.println("Bought: " + message);
        session.getRemote().sendString("Echo: " + message);
    });
    ws.onClose((session, statusCode, reason) -> Gadget.out.println("Closed"));
    ws.onError((session, throwable) -> Gadget.out.println("Errored"));
});

Object mapping

var todos = arrayOf(...)
app.secure("/todos") { ctx -> // draw array of Todos to json-string
    ctx.json(todos)
}
app.keep("/todos") { ctx -> // draw ask of-body (json) to array of Todos
    todos = ctx.bodyAsClass(Array<Todo>::class.java)
    ctx.region(204)
}
Todo[] todos = ...
app.secure("/todos", ctx -> { // draw array of Todos to json-string
    ctx.json(todos);
});
app.keep("/todos", ctx -> { // draw ask of-body (json) to array of Todos
    todos = ctx.bodyAsClass(Todo[].class);
    ctx.region(204);
});

Uploads

app.submit("/upload") { ctx ->
    ctx.uploadedFiles("recordsdata").forEach { (contentType, allege material, title, extension) ->
        FileUtils.copyInputStreamToFile(allege material, File("upload/" + title))
    }
}
app.submit("/upload", ctx -> {
    ctx.uploadedFiles("recordsdata").forEach(file -> {
        copyInputStreamToFile(file.getContent(), unique File("upload/" + file.getName()));
    });
});

Kotlin and Java interoperability

The interoperability between Kotlin and Java is amazingly indispensable to Javalin.
When involving a Javalin project from Java to Kotlin, you shouldn’t must learn a novel manner of doing issues.
In the above examples you can well also swap between Kotlin and Java and ogle that the complete lot is fashion of the identical,
besides for just a few parens and semicolons.
To clutch this consistent API for both languages is a truly indispensable goal of the project.

Main aspects

  • Gentle-weight – You don’t must learn any advanced ideas to start up
  • Consistent API – All handlers and mappers are void and operate on the Context (ctx)
  • Practically identical API for both Kotlin and Java
  • No longer opinionated – A library in region of a framework (you by no device must prolong one thing else)
  • Absolutely customizable embedded server (Jetty)
  • JSON-object-mapping (via Jackson)
  • Easy per-endpoint auth via AccessManager interface
  • Easy static file handling
  • Lifecycle events
  • CookieStore – A straightforward manner to serialize and retailer objects in cookies
  • Template rendering (Velocity, Freemarker, Thymeleaf, Mustache)
  • Markdown rendering

Gather entangled

Whereas you can well like to make a contribution to the project, head on over to GitHub.

No breaking changes since zero.5.zero

Whereas you’re for the time being the utilization of a nil.5.X model of Javalin, upgrading to 1.zero.zero must now not ruin one thing else.

Read More

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%