本指南将帮助您设置第一个 tinystruct 应用程序并了解基本工作流程。
- Java 开发工具包 (JDK) 17 或更高版本
- Maven(用于依赖管理)
- 文本编辑器或 IDE(IntelliJ IDEA、Eclipse、VS Code 等)
最简单的入门方式是使用 tinystruct archetype 创建项目:
# 按照 archetype 指南快速创建项目
https://github.com/tinystruct/tinystruct-archetype将 tinystruct 依赖项添加到项目的 pom.xml 文件中:
<dependency>
<groupId>org.tinystruct</groupId>
<artifactId>tinystruct</artifactId>
<version>1.7.18</version>
<classifier>jar-with-dependencies</classifier>
</dependency>或者,您可以直接从 Maven 仓库 下载 JAR 文件,并将其添加到项目的类路径中。
创建一个扩展 AbstractApplication 的新 Java 类:
package com.example;
import org.tinystruct.AbstractApplication;
import org.tinystruct.system.annotation.Action;
public class HelloWorldApp extends AbstractApplication {
@Override
public void init() {
// 初始化代码
}
@Override
public String version() {
return "1.0.0";
}
@Action("hello")
public String hello() {
return "你好,世界!";
}
@Action("hello")
public String hello(String name) {
return "你好," + name + "!";
}
// HTTP 特定方法 Action
@Action(value = "greet", mode = Action.Mode.HTTP_GET)
public String greetGet() {
return "你好 (GET)";
}
@Action(value = "greet", mode = Action.Mode.HTTP_POST)
public String greetPost() {
return "你好 (POST)";
}
}在项目的资源目录中创建 config.properties 文件:
# 应用程序设置
application.name=HelloWorldApp
application.mode=development
# 服务器设置
server.port=8080
server.host=localhost
# 默认设置
default.file.encoding=UTF-8
default.home.page=hello/World
default.reload.mode=true
default.date.format=yyyy-MM-dd HH:mm:ss您可以使用 tinystruct 调度器从命令行运行应用程序:
# 显示版本
bin/dispatcher --version
# 运行 hello 动作
bin/dispatcher hello --import com.example.HelloWorldApp
# 使用参数运行
bin/dispatcher hello/John --import com.example.HelloWorldApp您可以在三种内置服务器中选择:
- Netty (推荐用于高性能环境)
bin/dispatcher start --import org.tinystruct.system.NettyHttpServer --import com.example.HelloWorldApp- Tomcat (传统 Servlet 支持)
bin/dispatcher start --import org.tinystruct.system.TomcatServer --import com.example.HelloWorldApp- Undertow (轻量级嵌入式)
bin/dispatcher start --import org.tinystruct.system.UndertowServer --import com.example.HelloWorldApp然后访问您的应用程序:
典型的 tinystruct 项目结构如下所示:
my-app/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ ├── HelloWorldApp.java
│ │ │ └── ...
│ │ └── resources/
│ │ └── config.properties
│ └── test/
│ └── java/
│ └── com/
│ └── example/
│ └── HelloWorldAppTest.java
├── bin/
│ └── dispatcher
└── pom.xml