Spring-Web的好处是,快速上手、快速成型,且成熟稳定无Bug。
但对于个人而言,这套框架太重了。由此探寻更好的轻量Web框架Jooby!
本文源码地址meethigher/jooby-example: 基于Netty的轻量级Web框架Jooby使用示例
创建原生maven空项目,引入依赖
<dependency>
<groupId>io.joobygroupId>
<artifactId>jooby-nettyartifactId>
<version>2.16.1version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.12version>
dependency>
创建启动类
import io.jooby.Jooby;
public class Application extends Jooby {
//构造函数快
{
get("/", ctx -> "Hello World");
}
public static void main(String[] args) {
runApp(args, Application.class);
}
}
Jooby同样支持解析多种类型的配置文件,只记录常用的application.conf
Jooby读取配置文件优先级顺序
System.getProperty("user.dir")
resources添加application.conf
server.port = 8080
app.name = "内部"
database.url = "jdbc:mysql://localhost:3306/mydb"
示例接口
import io.jooby.Jooby;
import io.jooby.MediaType;
import java.nio.charset.StandardCharsets;
public class Application extends Jooby {
//构造函数快
{
// use Decorator.
decorator(next -> ctx -> {
// get response content-type.
final MediaType responseType = ctx.getResponseType();
// if content-type is text-specific
if(responseType.isTextual())
// override response type with current media type and always use UTF-8 charset!
ctx.setResponseType(responseType, StandardCharsets.UTF_8);
// pipe next response procedure.
return next.apply(ctx);
});
get("/", ctx -> {
String appName = getConfig().getString("app.name");
return "Welcome to " + appName;
});
get("/database", ctx -> {
String dbUrl = getConfig().getString("database.url");
return "Database URL: " + dbUrl;
});
}
public static void main(String[] args) {
runApp(args, Application.class);
}
}
Script API 通常用于编写小型的、单一用途的路由处理器。
支持占位符路由规则,但是像拼参形式的就不支持。
import static io.jooby.Jooby.runApp;
public class ScriptAPIApplication {
public static void main(String[] args) {
//consumer函数式接口简写
runApp(args, app -> {
app.get("/param/{id}", ctx -> {
String id = ctx.path("id").value();
return id;
});
app.post("/param/{id}", ctx -> {
return ctx.path("id").value();
});
});
}
}
添加依赖
<dependency>
<groupId>io.joobygroupId>
<artifactId>jooby-aptartifactId>
<version>2.16.1version>
dependency>
<dependency>
<groupId>io.joobygroupId>
<artifactId>jooby-gsonartifactId>
<version>2.16.1version>
dependency>
创建controller
import io.jooby.MediaType;
import io.jooby.Multipart;
import io.jooby.annotations.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@Path("/mvc")
public class Controller {
@GET("/get")
public String get() {
return "Hello World";
}
@GET("/pathParam/{id}")
public String pathParam(@PathParam String id) {
return id;
}
@GET("/queryParam")
public String queryParam(@QueryParam String id, @QueryParam String name) {
return name + "--->" + id;
}
/**
* form表单,支持两种形式
* application/x-www-form-urlencoded,不支持文件
* multipart/form-data,支持文件
*/
@POST("/formParam")
public String formParam(@FormParam String name, @FormParam String age, @FormParam Multipart file) {
if (file != null) {
try (InputStream is = file.file("file").stream(); FileOutputStream fos = new FileOutputStream("test.txt")) {
int b;
while ((b = is.read()) != -1) {
fos.write(b);
}
fos.flush();
} catch (IOException e) {
}
}
return name + "--->" + age;
}
@POST("/bodyParam")
@Consumes(MediaType.JSON)//可省略
@Produces(MediaType.JSON)//可省略
public Object bodyParam(Object body) {
return body;
}
@POST("/pageQuery")
public String pageQuery(PageRequest req) {
return req.toString();
}
}
class PageRequest {
private Integer pageIndex;
private Integer pageSize;
public Integer getPageIndex() {
return pageIndex;
}
public void setPageIndex(Integer pageIndex) {
this.pageIndex = pageIndex;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
注册controller
import io.jooby.json.GsonModule;
import top.meethigher.mvc.controller.Controller;
import static io.jooby.Jooby.runApp;
public class MVCApplication {
public static void main(String[] args) {
runApp(args, app -> {
// 设置 JSON 支持
app.install(new GsonModule());
//注册控制器
app.mvc(new Controller());
});
}
}
jooby:做更多! 更容易!
// 设置 JSON 支持
app.install(new GsonModule());
//注册控制器
app.mvc(new Controller());
});
}
}
[外链图片转存中...(img-mFU1u5le-1687016527518)]
# 四、参考致谢
[jooby:做更多! 更容易!](https://jooby.io/#introduction)