基于jdk内部的httpserver封装的轻量级http服务端
- 非常轻量,代码简单,大小只有12k
- api友好,用的很爽
- 支持8+以上的所有版本,支持native构建
Maven
<dependency>
<groupId>io.github.kongweiguang</groupId>
<artifactId>kHTTP</artifactId>
<version>0.0.3</version>
</dependency>
Gradle
implementation 'io.github.kongweiguang:KHTTP:0.0.3'
Gradle-Kotlin
implementation("io.github.kongweiguang:kHTTP:0.0.3")
public class Test1 {
public static void main(String[] args) {
KHTTP.of()
.executor(Executors.newCachedThreadPool())
.web("/Users/kongweiguang/Desktop/hegui/xm/gs")
.get("/get", (req, res) -> {
final MultiValueMap<String, String> params = req.params();
res.send("hello").send("world");
})
.post("/post", ((req, res) -> {
final String str = req.str();
System.out.println("str = " + str);
res.send("{\"key\":\"i am post res\"}");
}))
.post("/upload", (req, res) -> {
final MultiValueMap<String, String> params = req.params();
final Map<String, List<UpFile>> files = req.fileMap();
})
.get("/xz", new Handler() {
@Override
public void doHandler(final Req req, final Res res) throws IOException {
res.file("k.txt", Files.readAllBytes(Paths.get("D:\\k\\k.txt")));
}
})
.ok(8080);
}
}