在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):javalin/javalin开源软件地址(OpenSource Url):https://github.com/javalin/javalin开源编程语言(OpenSource Language):Kotlin 82.3%开源软件介绍(OpenSource Introduction):Javalin - A simple web framework for Java and KotlinJavalin 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:
General information:
QuickstartAdd dependencyMaven<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>4.6.4</version>
</dependency> Gradleimplementation "io.javalin:javalin:4.6.4" Start programming (Java)import io.javalin.Javalin;
public class HelloWorld {
public static void main(String[] args) {
Javalin app = Javalin.create().start(7000);
app.get("/", ctx -> ctx.result("Hello World"));
}
} Start programming (Kotlin)import io.javalin.Javalin
fun main() {
val app = Javalin.create().start(7000)
app.get("/") { ctx -> ctx.result("Hello World") }
} ExamplesThis section contains a few examples, mostly just extracted from the docs. All examples are in Kotlin, but you can find them in Java in the documentation (it's just syntax changes). Api structure and server configval app = Javalin.create { config ->
config.defaultContentType = "application/json"
config.autogenerateEtags = true
config.staticFiles.add("/public")
config.asyncRequestTimeout = 10_000L
config.dynamicGzip = true
config.enforceSsl = true
}.routes {
path("users") {
get(UserController::getAll)
post(UserController::create)
path(":user-id") {
get(UserController::getOne)
patch(UserController::update)
delete(UserController::delete)
}
ws("events", userController::webSocketEvents)
}
}.start(port) WebSocketsapp.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 Mappersapp.before("/some-path/*") { ctx -> ... } // runs before requests to /some-path/*
app.before { ctx -> ... } // runs before all requests
app.after { ctx -> ... } // runs after all requests
app.exception(Exception.class) { e, ctx -> ... } // runs if uncaught Exception
app.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 events
app.wsAfter { ws -> ... } // runs after all ws events
app.wsException(Exception.class) { e, ctx -> ... } // runs if uncaught Exception in ws handler JSON-mappingvar 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 uploadsapp.post("/upload") { ctx ->
ctx.uploadedFiles("files").forEach { (contentType, content, name, extension) ->
FileUtil.streamToFile(content, "upload/$name")
}
} Special thanks
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论