Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A simple and modern Java and Kotlin web framework

License

NotificationsYou must be signed in to change notification settings

javalin/javalin

Repository files navigation

Logo

A simple web framework for Java and Kotlin


Static BadgeStable VersionSnapshot VersionDiscord Link

Javalin is a very lightweight web framework for Kotlin and Java which supports WebSockets, HTTP2 and async requests.Javalin’s main goals are simplicity, a great developer experience, and first class interoperability between Kotlin and Java.

Javalin is more of a library than a framework. Some key points:

  • You don't need to extend anything
  • There are no @Annotations
  • There is no reflection
  • There is no other magic; just code.

General information

Community

We have a very activeDiscord server where you can get help quickly. We also have a (much lessactive)Slack if you prefer that.

Contributions are very welcome, you can read more about contributing in our guide:CONTRIBUTING

Please consider❤️ Sponsoring or starring Javalin if you want to support the project.

Quickstart

Add dependency

Maven

<dependency>    <groupId>io.javalin</groupId>    <artifactId>javalin</artifactId>    <version>6.7.0</version></dependency>

Gradle

implementation("io.javalin:javalin:6.7.0")

Start programming (Java)

importio.javalin.Javalin;publicclassHelloWorld {publicstaticvoidmain(String[]args) {varapp =Javalin.create(/*config*/)            .get("/",ctx ->ctx.result("Hello World"))            .start(7070);    }}

Start programming (Kotlin)

importio.javalin.Javalinfunmain() {val app=Javalin.create(/*config*/)        .get("/") { it.result("Hello World") }        .start(7070)}

Examples

This section contains a few examples, mostly just extracted from thedocs.All examples are in Kotlin, but you can find them in Java in the documentation (it's just syntax changes).

You can find more examples in thejavalin-samples repository.

Api structure and server config

importio.javalin.Javalinimportio.javalin.apibuilder.ApiBuilder.*funmain() {val app=Javalin.create { config->        config.useVirtualThreads=true        config.http.asyncTimeout=10_000L        config.staticFiles.add("/public")        config.staticFiles.enableWebjars()        config.router.apiBuilder {            path("/users") {                get(UserController::getAll)                post(UserController::create)                path("/{userId}") {                    get(UserController::getOne)                    patch(UserController::update)                    delete(UserController::delete)                }                ws("/events", userController::webSocketEvents)            }        }    }.start(7070)}

WebSockets

app.ws("/websocket/{path}") { ws->    ws.onConnect { ctx->println("Connected") }    ws.onMessage { ctx->val user= ctx.message<User>()// convert from json string to object        ctx.send(user)//  convert to json string and send back    }    ws.onClose { ctx->println("Closed") }    ws.onError { ctx->println("Errored") }}

Filters and Mappers

app.before("/some-path/*") { ctx->... }// runs before requests to /some-path/*app.before { ctx->... }// runs before all requestsapp.after { ctx->... }// runs after all requestsapp.exception(Exception.class) { e, ctx->... }// runs if uncaught Exceptionapp.error(404) { ctx->... }// runs if status is 404 (after all other handlers)app.wsBefore("/some-path/*") { ws->... }// runs before ws events on /some-path/*app.wsBefore { ws->... }// runs before all ws eventsapp.wsAfter { ws->... }// runs after all ws eventsapp.wsException(Exception.class) { e, ctx->... }// runs if uncaught Exception in ws handler

JSON-mapping

var todos= arrayOf(...)app.get("/todos") { ctx->// map array of Todos to json-string    ctx.json(todos)}app.put("/todos") { ctx->// map request-body (json) to array of Todos    todos= ctx.body<Array<Todo>>()    ctx.status(204)}

File uploads

app.post("/upload") { ctx->    ctx.uploadedFiles("files").forEach { uploadedFile->FileUtil.streamToFile(uploadedFile.content(),"upload/${uploadedFile.filename()}")    }}

Plugins

Javalin has a plugin system that allows you to add functionality to the core library.You can find a list of pluginshere.

Installing a plugin is as easy as adding a dependency to your project and registering it with Javalin:

Javalin.create { config->    config.registerPlugin(MyPlugin())}

Some of the most popular plugins are:

OpenAPI Plugin

TheJavalin OpenAPI plugin allows you to generate an OpenAPI 3.0 specification for your API at compile time.

Annotate your routes with@OpenApi to generate the specification:

@OpenApi(    summary="Get all users",    operationId="getAllUsers",    tags= ["User"],    responses= [OpenApiResponse("200", [OpenApiContent(Array<User>::class)])],    path="/users",    methods= [HttpMethod.GET])fungetAll(ctx:Context) {    ctx.json(UserService.getAll())}

Swagger UI and ReDoc UI implementations for viewing the generated specification in your browser are also available.

For more information, see theJavalin OpenAPI Wiki.

SSL Plugin

TheJavalin SSL plugin allows you to easily configure SSL for your Javalin server, supporting a variety of formats such as PEM, PKCS12, DER, P7B, and JKS.

Enabling SSL on the 443 port is as easy as:

val plugin=SSLPlugin { conf->    conf.pemFromPath("/path/to/cert.pem","/path/to/key.pem")}Javalin.create { javalinConfig->    javalinConfig.plugins.register(plugin)}.start()

Sponsors

LogoSponsorAmount
Barbary Software@barbarysoftware(50 USD/m)
Vasilis Soumakis@VassilisSoum(25 USD/m)

Special thanks


[8]ページ先頭

©2009-2025 Movatter.jp