A simple and modern Java and Kotlin web framework
译文:一个简单而现代的Java和Kotlin web框架
文档
项目示例
环境
# JDK版本1.8 $ java -version java version "1.8.0_251" Java(TM) SE Runtime Environment (build 1.8.0_251-b08) Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
Maven 项目结构
$ tree . ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── example │ │ ├── HelloWorld.java │ │ └── User.java │ └── resources └── test └── java
引入依赖
<dependency> <groupId>io.javalin</groupId> <artifactId>javalin</artifactId> <version>4.6.7</version> </dependency>
完整依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/io.javalin/javalin --> <dependency> <groupId>io.javalin</groupId> <artifactId>javalin</artifactId> <version>4.6.7</version> </dependency> <!--log--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.31</version> </dependency> <!--json--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.13.3</version> </dependency> </dependencies> </project>
User.java
package com.example; import com.fasterxml.jackson.annotation.JsonFormat; import java.time.LocalDateTime; public class User { private String name; private Integer age; @JsonFormat(pattern = "yyyy年MM月dd日") private LocalDateTime birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public LocalDateTime getBirthday() { return birthday; } public void setBirthday(LocalDateTime birthday) { this.birthday = birthday; } }
HelloWorld.java
package com.example; import io.javalin.Javalin; import java.time.LocalDateTime; import java.util.HashMap; public class HelloWorld { public static void main(String[] args) { Javalin app = Javalin.create(); app.get("/", ctx -> { User user = new User(); user.setAge(20); user.setName("Tom"); user.setBirthday(LocalDateTime.now()); ctx.json(user); }); app.start(7070); } }
请求地址:http://localhost:7070/
返回数据
{ "name": "Tom", "age": 20, "birthday": "2023年03月01日" }
对比
和SpringBoot相比还是差的有点远
框架 | star |
SpringBoot | |
Javalin |