# litejava
**Repository Path**: isee00/litejava
## Basic Information
- **Project Name**: litejava
- **Description**: No description available
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2025-12-27
- **Last Updated**: 2026-03-28
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# LiteJava
🚀 极简 Java Web 框架
性能比肩 Go,轻量高效,插件化扩展,无缝融合 Java 生态
快速开始 •
为什么选择 •
核心特性 •
插件生态 •
性能测试
---
## 30 秒上手
```java
import litejava.*;
import litejava.plugins.LiteJava;
import litejava.util.Maps;
public class Main {
public static void main(String[] args) {
App app = LiteJava.create();
app.get("/", ctx -> ctx.json(Maps.of("message", "Hello, LiteJava!")));
app.get("/users/:id", ctx -> {
long id = ctx.pathParamLong("id");
ctx.json(Maps.of("id", id, "name", "User " + id));
});
app.run(); // 启动!访问 http://localhost:8080
}
}
```
**就这么简单。** 没有 XML,没有注解地狱,没有 30 秒的启动等待。
---
## 为什么选择 LiteJava?
### 🎯 如果你厌倦了...
- **Spring Boot 的臃肿** - 启动 10 秒,内存 500MB,一个 Hello World 引入 100+ 依赖
- **注解的泛滥** - `@RestController` `@RequestMapping` `@Autowired` `@Service` `@Component`...
- **魔法般的自动装配** - 出了问题不知道哪里错,堆栈 50 层看不懂
- **配置的复杂** - application.yml 写了 200 行还没配完
### ✨ LiteJava 给你...
| 痛点 | Spring Boot | LiteJava |
|------|-------------|----------|
| 启动时间 | 3-10 秒 | **< 500ms** |
| 内存占用 | 200-500 MB | **30-80 MB** |
| JAR 大小 | 30-100 MB | **< 1 MB** (core) |
| 依赖数量 | 100+ | **0** (core) |
| 学习曲线 | 陡峭(注解+约定太多) | **平缓**(代码即配置) |
| 调试难度 | 困难(魔法太多) | **简单**(所见即所得) |
---
## 核心特性
### 1️⃣ Gin-style 路由
```java
// 基础路由
app.get("/users", ctx -> ctx.json(userService.list()));
app.post("/users", ctx -> ctx.json(userService.create(ctx.bindJSON())));
app.put("/users/:id", ctx -> ctx.json(userService.update(ctx.pathParamLong("id"), ctx.bindJSON())));
app.delete("/users/:id", ctx -> ctx.ok(userService.delete(ctx.pathParamLong("id"))));
// 路由分组 - 告别重复前缀
app.group("/api/v1", api -> {
api.get("/books", BookController::list); // GET /api/v1/books
api.post("/books", BookController::create); // POST /api/v1/books
api.get("/books/:id", BookController::get); // GET /api/v1/books/:id
});
// 嵌套分组 + 分组级中间件
app.group("/admin", admin -> {
admin.use(new AuthPlugin(token -> jwtPlugin.verify(token))); // 只对 /admin/* 生效
admin.group("/users", users -> {
users.get("/", UserController::list);
users.delete("/:id", UserController::delete);
});
});
// 通配符路由
app.get("/files/*filepath", ctx -> ctx.file(new File(uploadDir, ctx.pathParam("filepath"))));
```
### 2️⃣ Koa-style 洋葱中间件
```java
// 请求日志中间件
app.use((ctx, next) -> {
long start = System.currentTimeMillis();
System.out.println("--> " + ctx.method + " " + ctx.path);
next.run(); // 执行后续中间件和 handler
long cost = System.currentTimeMillis() - start;
System.out.println("<-- " + ctx.status + " " + cost + "ms");
});
// 认证插件 - 使用内置 AuthPlugin
app.use(new AuthPlugin(token -> {
// 自定义验证逻辑,返回用户信息 Map 或 null
return jwtPlugin.verify(token);
}).whitelist("/", "/login").whitelistPrefix("/static"));
// 在 Handler 中获取认证信息
app.get("/api/me", ctx -> {
Map user = (Map) ctx.state.get("auth");
ctx.ok(user);
});
```
### 3️⃣ 简洁的 Context API
```java
app.post("/users", ctx -> {
// 获取参数
String name = ctx.queryParam("name"); // 查询参数
int page = ctx.queryParamInt("page", 1); // 带默认值
long id = ctx.pathParamLong("id"); // 路径参数
String token = ctx.header("Authorization"); // 请求头
User user = ctx.bindJSON(User.class); // JSON 请求体
// 响应
ctx.ok(data); // {"code":0, "data":..., "msg":"success"}
ctx.fail("error message"); // {"code":-1, "msg":"error message"}
ctx.json(obj); // 原始 JSON
ctx.text("hello"); // 纯文本
ctx.html("Hi
"); // HTML
ctx.redirect("/login"); // 重定向
ctx.file(new File("doc.pdf")); // 文件下载
ctx.render("user.html", model); // 模板渲染
});
```
### 4️⃣ 万物皆插件
**不强制任何功能,自由装配你需要的插件:**
```java
// 最小化启动 - 只要路由和服务器
App app = new App();
app.use(new HttpServerPlugin());
app.get("/", ctx -> ctx.text("Hello"));
app.run();
// 按需添加功能
app.use(new JacksonPlugin()); // 需要 JSON?
app.use(new JdbcPlugin()); // 需要数据库?
app.use(new RedisCachePlugin()); // 需要缓存?
app.use(new ThymeleafPlugin()); // 需要模板?
app.use(new SwaggerPlugin()); // 需要 API 文档?
// 或者一键启动(预装常用插件)
App app = LiteJava.create(); // 包含 Jackson + MemoryCache + HttpServer
```
**插件可随时替换,对业务代码零影响:**
```java
// 开发环境:用内置 HttpServer
app.use(new HttpServerPlugin());
// 生产环境:换成 Netty 高性能服务器
app.use(new NettyServerPlugin());
// 或者用虚拟线程版本 (Java 21+)
app.use(new JdkVirtualThreadServerPlugin());
```
**有趣的工具插件,开发调试更轻松:**
```java
// DebugPlugin - 启动时打印应用结构
app.use(new DebugPlugin());
// 输出:
// ╔══════════════════════════════════════════════════════════════╗
// ║ My Application ║
// ╠══════════════════════════════════════════════════════════════╣
// ║ Plugins (6): ║
// ║ 1. Slf4jLogPlugin ║
// ║ 2. JacksonPlugin ║
// ║ 3. HttpServerPlugin ║
// ╠══════════════════════════════════════════════════════════════╣
// ║ Middleware Chain (2): ║
// ║ Request → [ExceptionPlugin] → [CorsPlugin] → Handler ║
// ╚══════════════════════════════════════════════════════════════╝
```
**配置也可以装在插件里:**
```java
// 默认:.properties 配置
app.use(new ConfPlugin()); // app.conf.get("key")
// 想用 YAML?换个插件
app.use(new YamlConfPlugin()); // 同样的 API,不同的配置格式
// 想从环境变量读取?自己写个插件
app.use(new EnvConfPlugin()); // 插件机制让扩展变得简单
```
---
## 真实项目示例
### RESTful API 服务
```java
public class BookApp {
public static void main(String[] args) {
App app = LiteJava.create();
// 数据库
HikariPlugin hikari = new HikariPlugin();
app.use(hikari);
JdbcPlugin jdbc = new JdbcPlugin(hikari);
app.use(jdbc);
// 图书 CRUD
app.group("/api/books", books -> {
books.get("/", ctx -> {
int page = ctx.queryParamInt("page", 1);
int size = ctx.queryParamInt("size", 20);
List