入门示例
新建工程
name:tio-boot-web-hello 开源地址
- https://github.com/litongjava/tio-boot-web-hello
添加依赖
The package is distributed through Maven Central. tio-boot,
If you are developing with Java 8, please use the following dependency:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<graalvm.version>23.1.1</graalvm.version>
<lombok-version>1.18.30</lombok-version>
<tio-boot.version>1.6.2</tio-boot.version>
<jfinal-aop.version>1.2.7</jfinal-aop.version>
<fastjson2.version>2.0.52</fastjson2.version>
<hotswap-classloader.version>1.2.4</hotswap-classloader.version>
<final.name>web-hello</final.name>
<main.class>com.litongjava.tio.web.hello.HelloApp</main.class>
</properties>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>tio-boot</artifactId>
<version>${tio-boot.version}</version>
</dependency>
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>hotswap-classloader</artifactId>
<version>${hotswap-classloader.version}</version>
</dependency>
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>jfinal-aop</artifactId>
<version>${jfinal-aop.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok-version}</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<!-- development -->
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.4</version>
<configuration>
<fork>true</fork>
<mainClass>${main.class}</mainClass>
<excludeGroupIds>org.projectlombok</excludeGroupIds>
<arguments>
<argument>--mode=dev</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- production -->
<profile>
<id>production</id>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.4</version>
<configuration>
<mainClass>${main.class}</mainClass>
<excludeGroupIds>org.projectlombok</excludeGroupIds>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Controller and Hanlder
TioBoot 支持两种类型的接口 Controller 和 Handler,两者的区别是
- Controller:支持请求参数封装,支持路由扫描
- Handler:不支持请求参数封装,需要手动从 HttpRequest 中获取参数,不支持路由扫描,需要手动配置路由,但是性能较高
建议 如果是系统级别的接口,建议编写 Handler,否则可以编写 Controller.
Controller 入门示例
因为 Controller 需要由启动类扫描,扫描后才能加载,所以需要再启动类上 添加 @AComponentScan
package com.litongjava.tio.web.hello;
import com.litongjava.jfinal.aop.annotation.AComponentScan;
import com.litongjava.tio.boot.TioApplication;
@AComponentScan
public class HelloApp {
public static void main(String[] args) {
long start = System.currentTimeMillis();
TioApplication.run(HelloApp.class, args);
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
}
IndexController
package com.litongjava.tio.web.hello;
import com.litongjava.tio.http.server.annotation.RequestPath;
@RequestPath("/")
public class IndexController {
@RequestPath()
public String index() {
return "index";
}
}
Handler 入门示例
因为 Handler 不需要由启动类扫描,所以不需要再启动类上 添加 @AComponentScan
import java.util.HashMap;
import java.util.Map;
import com.litongjava.tio.boot.http.TioRequestContext;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import com.litongjava.tio.utils.resp.RespVo;
public class HelloHandler {
public HttpResponse hello(HttpRequest request) {
Map<String, String> data = new HashMap<>();
RespVo respVo = RespVo.ok(data);
return TioRequestContext.getResponse().setJson(respVo);
}
}
WebHelloConfig
import java.io.IOException;
import com.litongjava.speed.server.handler.HelloHandler;
import com.litongjava.tio.boot.context.TioBootConfiguration;
import com.litongjava.tio.boot.server.TioBootServer;
import com.litongjava.tio.http.server.router.RequestRoute;
public class WebHelloConfig implements TioBootConfiguration {
@Override
public void config() throws IOException {
HelloHandler speedHandler = new HelloHandler();
RequestRoute r = TioBootServer.me().getRequestRoute();
r.add("/hello", speedHandler::hello);
}
}
HelloApp
import com.litongjava.speed.server.config.WebHelloConfig;
import com.litongjava.tio.boot.TioApplication;
public class HelloApp {
public static void main(String[] args) {
long start = System.currentTimeMillis();
TioApplication.run(HelloApp.class, new WebHelloConfig(), args);
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
}
output
{ "data": {}, "code": 1, "msg": null, "ok": true }
使用 spring-boot 插件启动
mvn spring-boot:run -Pproduction
访问测试 http://localhost/,显示 index
打包
mvn clean package -DskipTests -Pproduction
启动
java -jar target\**.jar