开发第一个 Web 应用
本教程使用 TODAY Infrastructure 和 Netty 构建一个 Web 应用。启动后,访问 GET / 即可收到 Hello World!。
选择 Gradle 或 Maven 中的一种构建方式,两种方式使用同一份 Java 代码。
使用 Gradle 构建
创建 hello-web 目录,并在其中创建 settings.gradle,使项目名和打包文件名不依赖目录名:
rootProject.name = 'hello-web'
在同一目录创建 build.gradle。应用插件会自动应用 Java 插件并导入同版本的 infra-dependencies BOM,因此各个 Starter 不必分别指定版本。
当前文档使用快照版本;示例通过 buildscript 直接加载插件,不依赖 Gradle 插件门户。
buildscript {
repositories {
mavenLocal { mavenContent { snapshotsOnly() } }
mavenCentral()
maven {
url = uri('https://central.sonatype.com/repository/maven-snapshots/')
mavenContent { snapshotsOnly() }
}
}
dependencies {
classpath 'cn.infra-tech:infra-gradle-plugin:1.0-Draft.1-SNAPSHOT'
}
}
apply plugin: 'cn.infra-tech.application'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
tasks.withType(JavaCompile).configureEach {
options.release = 17
}
repositories {
mavenLocal { mavenContent { snapshotsOnly() } }
mavenCentral()
maven {
url = uri('https://central.sonatype.com/repository/maven-snapshots/')
mavenContent { snapshotsOnly() }
}
}
dependencies {
implementation 'cn.infra-tech:infra-starter-application'
implementation 'cn.infra-tech:infra-starter-webmvc'
implementation 'cn.infra-tech:infra-starter-netty-server'
}
插件的其他应用方式见 Gradle 插件快速入门。
使用 Maven 构建
创建 hello-web 目录,并在其中创建 pom.xml。infra-dependencies BOM 统一管理依赖版本,infra-maven-plugin 提供启动和打包功能。
当前文档使用快照版本;repositories 用于解析 BOM 和 Starter,pluginRepositories 用于解析 Maven 插件。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>hello-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<infra.version>1.0-Draft.1-SNAPSHOT</infra.version>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.infra-tech</groupId>
<artifactId>infra-dependencies</artifactId>
<version>${infra.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.infra-tech</groupId>
<artifactId>infra-starter-application</artifactId>
</dependency>
<dependency>
<groupId>cn.infra-tech</groupId>
<artifactId>infra-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>cn.infra-tech</groupId>
<artifactId>infra-starter-netty-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
<plugin>
<groupId>cn.infra-tech</groupId>
<artifactId>infra-maven-plugin</artifactId>
<version>${infra.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>sonatype-snapshots</id>
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>sonatype-snapshots</id>
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</project>
添加 Hello World 代码
在项目目录下创建 src/main/java/demo/DemoApplication.java:
package demo;
import infra.app.Application;
import infra.app.InfraApplication;
import infra.web.annotation.GET;
import infra.web.annotation.RestController;
@InfraApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
Application.run(DemoApplication.class, args);
}
@GET("/")
public String hello() {
return "Hello World!";
}
}
@GET("/") 将 GET / 映射到 hello()。@RestController 让方法返回的字符串直接写入 HTTP 响应体。
更多路由和控制器用法见 Web MVC。
@InfraApplication 注解
@InfraApplication 组合了 @Configuration、@EnableAutoConfiguration 和 @ComponentScan。
它将 DemoApplication 声明为配置类,开启自动配置,并默认从 demo 包开始扫描组件。
本例中,infra-starter-application 提供应用启动所需的依赖,infra-starter-webmvc 引入 Web MVC 组件,infra-starter-netty-server 引入 Netty Web 服务器。
框架根据类路径上的这些组件和应用配置,自动配置 Web MVC 并启动服务器。
验证应用
启动日志显示服务器已监听 8080 后,在浏览器中访问 http://localhost:8080/,或打开另一个终端执行:
curl -fsS http://localhost:8080/
响应如下:
Hello World!
打包并重新启动之前,先在启动应用的终端按 Ctrl+C 停止应用;如果从 IDE 启动,则使用 IDE 的停止按钮。
使用 Maven 打包可执行 JAR
mvn package 会执行 POM 中配置的 repackage 目标,将应用及其运行时依赖一起打包。
mvn package
java -jar target/hello-web-0.0.1-SNAPSHOT.jar
无论使用哪种构建方式,都可以再次访问 localhost:8080/ 验证打包后的应用。