Skip to content

SmartWeb 快速开始

下面使用 Kotlin/Java 和 SmartHTTP 创建一个最小 HTTP 服务。示例版本以当前源码为准;若制品仓库未发布该版本,请使用仓库实际可用版本。

添加依赖

kotlin
repositories {
    mavenCentral()
    maven("https://maven.cnb.cool/IceCream/maven/-/packages/")
}

dependencies {
    implementation("com.IceCreamQAQ.SmartWeb:SmartWeb:1.0.0-DEV11")
    implementation("com.IceCreamQAQ.SmartWeb.Server:SmartHTTP:1.0.0-DEV11")
}

服务器模块会提供默认服务器实现;应用仍需具备 Rain 完整运行时所需的依赖和配置。

创建入口

kotlin
import rain.application.FullStackApplicationLauncher

fun main() {
    FullStackApplicationLauncher.launch()
}

创建控制器

kotlin
import rain.controller.annotation.Path
import smartweb.annotation.GetAction
import smartweb.annotation.PostAction
import smartweb.annotation.WebController

@WebController
@Path("users")
class UserController {
    @GetAction("{id}")
    fun getUser(id: Long) = mapOf("id" to id)

    @PostAction
    fun createUser(name: String) = mapOf("name" to name)
}

@Path 设置控制器前缀,Action 注解设置相对路径。路径中的 {id} 会映射到同名方法参数。

添加配置

src/main/resources/conf/project.properties 中写入:

properties
rain.scanPackages=com.example
webServer.port=8080

启动应用后,示例控制器会注册 GET /users/{id}POST /users。实际 URL 是否包含额外根路径,取决于应用的 Controller 配置。

下一步

基于 Apache License 2.0 发布