Javalin(https://github.com/tipsy/javalin/)是一款非常适合 Kotlin 和 Java 程序员的轻量级 Web 框架,目前稳定版本为
Javalin2.8.0
。Javalin 主要有以下的特点:
轻量级:不用提前学习任何概念就可以开始使用
一致的 API:所有的处理程序和映射器在 Context (ctx)中都是无效的。
Kotlin 和 Java 拥有几乎完全相同的 API
是框架也是库:无需扩展任何功能
拥有完全可定制的嵌入式服务器(Jetty)
JSON 对象映射
通过 AccessManager 接口简单的按端点验证
简单的静态文件处理
生命周期事件
CookieStore——一种简单的用来序列化的方法和存储在 cookie 中的对象。
模板渲染
Markdown 渲染
为什么要使用Javalin呢?因为我看到了这篇文章 请给Sprint Boot多一些内存
所以今天,我就来试试
<dependencies>
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>2.8.0</version>
</dependency>
<!-- json解析支持 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<!-- log组件 可以不要 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.26</version>
</dependency>
</dependencies>
public class Application {
public static void main(String[] args) {
Javalin app = Javalin.create().start(7000);
app.get("/", new Handler() {
@Override
public void handle(@NotNull Context context) throws Exception {
context.result("Hello World")
}
});
}
它的 API 特别简单
几个常用的方法 before
, get
, post
, put
, delete
, after
app.before("/some-path/*", ctx -> {
// 在所有请求 "/some-path/*" 前运行
});
app.before(ctx -> {
// 在所有请求前运行("/*", handler)
});
app.get("/",ctx -> {
});
public static void main(String[] args) {
Javalin app = Javalin.create().start(7000);
app.routes(() -> {
path("users", () -> {
get(UserController::getAllUsers);
post(UserController::createUser);
path(":id", () -> {
get(UserController::getUser);
patch(UserController::updateUser);
delete(UserController::deleteUser);
});
});
});
}
public class User implements Serializable {
private String name;
private String sex;
private Integer age;
//getter、setter 省略
}
public class UserController {
public static void getAllUsers(Context context) {
List<User> users = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setName("name"+i);
user.setSex("男");
user.setAge(10+i);
users.add(user);
}
context.json(users);
}
public static void createUser(Context context) {
User user = context.bodyAsClass(User.class);
context.json(user);
}
public static void getUser(Context context) {
//获取id
String id = context.pathParam("id");
User user = new User();
user.setName("name");
user.setSex("男");
user.setAge(10);
context.json(user);
}
public static void updateUser(Context context) {
//获取id
String id = context.pathParam("id");
context.result(id+" 更新成功了");
}
public static void deleteUser(Context context) {
//获取id
String id = context.pathParam("id");
context.result(id+" 删除成功了");
}
}
get "/users"
post "/users"
get "/users/:id"
patch "/users/:id"
delete "/users/:id"
在 Javalin
中上传文件非常简单
app.post("/upload", ctx -> {
ctx.uploadedFiles("files").forEach(file ->
FileUtil.streamToFile(file.getContent(), "upload/" + file.getName())
);
ctx.res.setCharacterEncoding("utf-8");
ctx.result("文件上传成功");
});
这样就写好了
在启动类上加上 enableStaticFiles("/classpath-folder")
Javalin app = Javalin
.create()
.enableStaticFiles("/classpath-folder")//开启静态资源
.start(7000);
在根目录下建个 classpath-folder
文件夹
在 classpath-folder
文件夹下建个 index.html
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="files" multiple>
<button>Submit</button>
</form>