本指南将引导您使用 FIT 编程框架构建一个简单的 Web 服务。
您将构建一个服务,该服务将接收一个 HTTP 的 GET 请求,该请求会以 Json 格式进行响应。同时通过增加 name 和 age参数值,来修改返回相应的内容。如发送请求 http://localhost:8080/user?name=zhangsan&age=18 ,则响应值如下:
{
"name": "zhangsan",
"age": 18,
"id": 1
}其中 id 属性值唯一标识返回的响应值。第一次 HTTP 请求调用后,响应值中 id 属性值为1,且每次调用后该值会自增1。
所需要的环境:
- 编辑器,如 IntelliJ IDEA
- Java 17
- Maven,推荐版本 Maven 3.8.8+
- fitframework 源码,在
framework/fit/java目录下使用 Maven 对框架进行编译:
mvn clean install
本指引以 IntelliJ IDEA 为例:
先在 IDEA 上创建一个 Maven 项目。
在系统生成的 pom 文件中,添加 Web 相关的依赖,如下所示:
<?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>org.fitframework</groupId>
<artifactId>simple-web-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<!-- FIT version -->
<fit.version>3.7.0-SNAPSHOT</fit.version>
<!-- Maven plugin versions -->
<maven.compiler.version>3.11.0</maven.compiler.version>
</properties>
<dependencies>
<dependency>
<groupId>org.fitframework</groupId>
<artifactId>fit-starter</artifactId>
<version>${fit.version}</version>
</dependency>
<dependency>
<groupId>org.fitframework</groupId>
<artifactId>fit-plugins-starter-web</artifactId>
<version>${fit.version}</version>
</dependency>
<dependency>
<groupId>org.fitframework</groupId>
<artifactId>fit-api</artifactId>
<version>${fit.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.fitframework</groupId>
<artifactId>fit-build-maven-plugin</artifactId>
<version>${fit.version}</version>
<executions>
<execution>
<id>package-app</id>
<goals>
<goal>package-app</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>其中添加了 fit-starter 依赖和 fit-plugins-starter-web 依赖,fit-starter 依赖用于应用的启动,fit-plugins-starter-web 依赖使得应用程序具备了 Web 服务的基本能力。fit-build-maven-plugin 模块用于项目的构建,<goal> 标签的 package-app 属性指示了打包应用这个构建指令,属于应用的必选指令。
我们在 main 方法中,通过 FitStarter 类的静态方法 start 去启动整个应用,示例如下:
package modelengine.fit.example;
import modelengine.fitframework.runtime.FitStarter;
/**
* 启动类。
*/
public class DemoApplication {
public static void main(String[] args) {
FitStarter.start(DemoApplication.class, args);
}
}注意类名应当与文件名保持一致,区分大小写。
考虑我们的目标要求,响应的服务能够处理 /user 的 Get 请求,并且在查询字符串中使用 name 、user 参数。所以我们可以创建个资源类 User ,同时含有属性 name 、user、id, 其中 id 值用来唯一标识 User对象 。该服务对象作为 HTTP 请求调用的返回值。
由于
fit-plugins-starter-web模块使用了基于 fastjson2 的序列化插件,所以应用程序会通过 fastjson2 的方式自动将User的实例对象封装成 Json 格式。我们也可以更改依赖,使用其他的插件来封装返回值。
package modelengine.fit.example.domain;
/**
* 表示用户资源类。
*/
public class User {
private final String name;
private final String age;
private final int id;
public User(String name, String age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
public String getName() {
return this.name;
}
public String getAge() {
return this.age;
}
public int getId() {
return id;
}
}在该应用程序中,HTTP 请求由控制器处理。我们可以创建相应的控制器和方法来进行处理,如下图:
package modelengine.fit.example.controller;
import modelengine.fit.example.domain.User;
import modelengine.fit.http.annotation.GetMapping;
import modelengine.fit.http.annotation.RequestParam;
import modelengine.fitframework.annotation.Component;
/**
* 表示用户资源的控制器。
*/
@Component
public class UserController {
private static int counter = 0;
@GetMapping(path = "/user")
public User getUser(@RequestParam("name") String name, @RequestParam("age") String age) {
return new User(name, age, ++counter);
}
}其中控制器资源类作为一个组件,需要在类上标识 @Component 注解。同时考虑到需要请求的 HTTP 调用是 Get 方法,所以在对应的调用方法上需要打上 @GetMapping 注解进行标识,并通过 path 属性确定 HTTP 的访问路径。我们也可以使用 @RequestMapping 注解,再加上 method = HttpRequestMethod.GET 属性,可以达到相同的效果。
@RequestParam 注解可以将 HTTP 请求的查询字符串的属性绑定到响应方法的字段,例如 HTTP 请求地址为 http://localhost:8080/user?name=zhangsan&age=18 ,则 getUser 的方法的参数 name 值为 zhangsan, age 值为 18。通过这种方法,我们就能获取请求的具体信息内容。
当前应用启动有两种方式:
- 通过 IDEA 启动:您可以直接在 IDEA 运行 main 方法。
- 通过执行 JAR 文件:您可以使用 Maven 在命令行运行,构建一个包含所有必要依赖项、类和资源的单个可执行 JAR 文件,并运行该文件。
下面简单介绍下执行 JAR 文件的步骤。首先需要使用 Maven 进行编译打包:
mvn clean install
编译命令执行后,会生成target目录,其中包含了可执行 JAR 文件。然后再运行 JAR 文件:
java -jar target/web-service-1.0-SNAPSHOT.jar
应用启动时,会有日志输出,来说明当前启动的情况。当出现如下的段落,说明启动已成功:
[yyyy-MM-dd hh:mm:ss.SSS] [INFO ] [main] [modelengine.fitframework.runtime.aggregated.AggregatedFitRuntime] Prepare to start FIT application...
[yyyy-MM-dd hh:mm:ss.SSS] [INFO ] [main] [modelengine.fitframework.runtime.aggregated.AggregatedFitRuntime] FIT application started.
[yyyy-MM-dd hh:mm:ss.SSS] [INFO ] [netty-http-server-thread-0] [modelengine.fit.http.server.netty.NettyHttpClassicServer] Start netty http server successfully. [httpPort=8080]
服务顺利启动后,可以通过地址 http://localhost:8080/user?name=zhangsan&age=18 访问资源。访问成功后,会出现如下的响应:
{
"name": "zhangsan",
"age": 18,
"id": 1
}如果重复发送 HTTP 请求,返回值的 id 字段值会随之增加,每次自增 1。
本章指南将引导您完成了使用 FIT 编程框架构建一个简单的 web 服务。其中本章并没有引入“插件”的概念,我们鼓励用户以面向接口编程的开发模式,打造插件式的应用系统,充分获取 FIT 编程框架的能力和优势。关于“插件化开发”的具体内容,可以阅读指南《构建插件式 Web 应用》。