Skip to content

Commit c6e6075

Browse files
author
litongjava
committed
add 06.md
1 parent 96a838a commit c6e6075

File tree

5 files changed

+204
-23
lines changed

5 files changed

+204
-23
lines changed

docs/.vuepress/config/sidebar-zh.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{
1919
"title": "03_配置",
2020
"collapsable": false,
21-
"children": ["03_配置/01.md", "03_配置/02.md", "03_配置/03.md", "03_配置/04.md", "03_配置/05.md"]
21+
"children": ["03_配置/01.md", "03_配置/02.md", "03_配置/03.md", "03_配置/04.md", "03_配置/05.md", "03_配置/06.md"]
2222
},
2323
{
2424
"title": "04_架构",

docs/zh/02_部署/04.md

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ export MVN_HOME=~/program/apache-maven-3.8.8/
3838
export PATH=$MVN_HOME/bin:$PATH
3939
```
4040

41+
3. 验证配置
42+
43+
```
44+
mvn -version
45+
```
46+
47+
```
48+
Apache Maven 3.8.8 (4c87b05d9aedce574290d1acc98575ed5eb6cd39)
49+
Maven home: /root/program/apache-maven-3.8.8
50+
Java version: 21.0.1, vendor: Oracle Corporation, runtime: /root/program/graalvm-jdk-21.0.1+12.1
51+
Default locale: en_US, platform encoding: UTF-8
52+
OS name: "linux", version: "5.15.0-92-generic", arch: "amd64", family: "unix"
53+
```
54+
4155
#### 打包成二进制文件
4256

4357
```

docs/zh/03_配置/06.md

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Json 转换
2+
3+
## 概述
4+
5+
tio-boot 的 json 模块以抽象类 Json 为核心,方便扩展第三方实现,tio-boot 官方给出了 4 个 Json 实现,分别是 TioJson、FastJson、Jackson,Gson 这 4 个实现继承自抽象类 Json。
6+
7+
抽象类 Json 的核心抽象如下:
8+
9+
```
10+
public abstract class Json {
11+
public abstract String toJson(Object object);
12+
public abstract <T> T parse(String jsonString, Class <T> type);
13+
}
14+
```
15+
16+
如上代码可以看出 Json 抽象就是 Object 与 json string 互转的两个方法,toJson(...)将任意 java 类型转成 json string,而 parse 将 json string 再反向转成范型指定的对象。
17+
18+
## Json 配置
19+
20+
tio-boot 官方提供了 Json 抽象类的三个实现:TioJson、FastJson、Jackson,如果不进行配置,那么默认使用 TioJson 实现,指定为其它实现需要在 configConstant 进行如下配置:
21+
22+
```
23+
JsonManager.me().setDefaultJsonFactory(new FastJsonFactory());
24+
```
25+
26+
上面配置将系统默认使用的 TioJson 切换到了 FastJson。还可以通过扩展 Json 抽象类以及 JsonFactory 来实现定制的 Json 实现。
27+
28+
假定用户扩展出了一个 MyJson 与 MyJsonFactory ,那么可以通过如下的方式切换到自己的实现上去:
29+
30+
```
31+
JsonManager.me().setJsonFactory(new MyJsonFactory());
32+
```
33+
34+
此外,tio-boot 官方还提供了 MixedJson、MixedJsonFactory 实现,这个实现让转 json string 时使用 TioJson,反向转成对象则使用 FastJson。
35+
如果希望在非 web 下进行配置,需要使用 JsonManager,例如:
36+
37+
```
38+
JsonManager.me().setJsonFactory(new MixedJsonFactory());
39+
```
40+
41+
还可以配置 Date 类型转 json 后的格式:
42+
43+
```
44+
JsonManager.me().setJsonDatePattern("yyyy-MM-dd");
45+
```
46+
47+
注意,在使用 MixedJsonFactory、FastJsonFactory、JacksonFactory 时需要添加其依赖,具体依赖参考下一小节内容。
48+
49+
## Json 的实现
50+
51+
官方默认给出了 5 种 json 实现:TioJson、FastJson、Jackson、MixedJson,Gson 可以满足绝大多数需求。
52+
53+
### 1、TioJson
54+
55+
TioJson 是 tio-boot 官方最早的一个实现,目前已经没有什么用
56+
57+
### 2、FastJson
58+
59+
FastJson 是对第三方的 fastjson2 进行的二次封装,该实现最重要的一点就是转换依赖于 Model、java bean 的 getter 方法。使用 fastjson 可以按照其官方文档去配置 fastjson 的各种转换参数。
60+
61+
使用 FastJson 封装时,需要添加其依赖:
62+
63+
```
64+
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
65+
<dependency>
66+
<groupId>com.alibaba.fastjson2</groupId>
67+
<artifactId>fastjson2</artifactId>
68+
<version>${fastjson.version}</version>
69+
</dependency>
70+
```
71+
72+
### 3、Jackson
73+
74+
该实现与 FastJson 类似,是对第三方的 jackson 的二次封装
75+
76+
使用 Jackson 封装时,需要添加其依赖:
77+
78+
```
79+
<dependency>
80+
<groupId>com.fasterxml.jackson.core</groupId>
81+
<artifactId>jackson-databind</artifactId>
82+
<version>2.11.0</version>
83+
</dependency>
84+
```
85+
86+
### 4、Gson
87+
88+
该实现与 FastJson 类似,是对第三方的 Gson 的二次封装
89+
90+
使用 Gson 封装时,需要添加其依赖:
91+
92+
```
93+
<dependency>
94+
<groupId>com.google.code.gson</groupId>
95+
<artifactId>gson</artifactId>
96+
<version>2.10.1</version>
97+
</dependency>
98+
```
99+
100+
### 5、MixedJson
101+
102+
MixedJson 是对 TioJson、FastJson 的再一次封装,Object 转 json string 时使用 TioJson 的实现,而反向 json string 转 Object 使用 FastJson。
103+
104+
这个实现结合了 TioJson 与 FastJson 两者的优势。 前者不支持 json string 到 Object 的转换,后者不支持关联表 sql 查询动态字段的转换。
105+
106+
使用 MixedJson 封装时需要添加 FastJson 封装的依赖:
107+
108+
```
109+
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
110+
<dependency>
111+
<groupId>com.alibaba.fastjson2</groupId>
112+
<artifactId>fastjson2</artifactId>
113+
<version>${fastjson.version}</version>
114+
</dependency>
115+
```
116+
117+
## 12.4 Json 转换用法
118+
119+
json 转换在 tio-boot 中的使用分为两类用法,第一类是使用配置的 json 转换,第二类是指定某个实现进行 json 转换。
120+
121+
### 1、使用配置的 json 实现转换
122+
123+
如下代码将使用前面章节中介绍的配置的 json 实现进行转的换:
124+
125+
1. 使用 Json 类进行 json 转换
126+
127+
```
128+
Json.getJson().toJson(object)
129+
Json.getJson().parse(jsonString, type);
130+
```
131+
132+
2. 使用 JsonUtils 工具类进行转换
133+
134+
```
135+
import com.litongjava.tio.utils.json.JsonUtils
136+
137+
JsonUtils.toJson(object)
138+
JsonUtils.parse(jsonString, type);
139+
```
140+
141+
3、使用指定的 json 实现转换
142+
143+
如果下代码将使用指定的 json 实现去转换:
144+
145+
```
146+
// 临时指定使用 FastJson 实现
147+
FastJson.getJson().toJson(...);
148+
FastJson.getJson().parse(...);
149+
150+
```
151+
152+
4. Action 直接返回对象今后转换
153+
154+
```
155+
public Student get(){
156+
return new Studnet();
157+
}
158+
```
159+
160+
上面这种用法可以临时摆脱配置的 json 实现,从而使用指定的 json 实现。

docs/zh/06_内置组件/01.md

+26-20
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,34 @@
3232

3333
添加一个在服务器关闭时执行的方法。这允许开发者注册自定义的资源清理逻辑。
3434

35-
## 获取和设置配置
35+
## 常用类解释
3636

37-
- `getTioServer()` / `getWsServerConfig()` / `getHttpConfig()`: 获取当前的服务器,WebSocket,HTTP 配置
37+
1. **`tioServer`**: 这可能是 T-io 服务器的一个实例。它作为服务器的中心运行点,处理进来的连接、数据以及管理资源
3838

39-
- `setServerInteceptorConfigure(ServerInteceptorConfigure)`: 设置服务器拦截器配置。
40-
- `setTioBootHttpRoutes(TioBootHttpRoutes)`: 设置 Tio Boot 的 HTTP 路由。
41-
- `setHttpRoutes(HttpRoutes)`: 设置 HTTP 路由。
42-
- `setServerTcpHandler(ServerTcpHandler)`: 设置 TCP 处理器。
43-
- `setServerAioListener(ServerAioListener)`: 设置服务器监听器。
39+
2. **`wsServerConfig`**: 这代表 WebSocket 服务配置。它包含与 WebSocket 协议相关的设置,如连接超时时间、消息缓冲区大小等。
4440

45-
- `getServerInteceptorConfigure`
46-
- `getTioBootHttpRoutes`
47-
- `getHttpRoutes`
48-
- `getServerTcpHandler`
49-
- `getServerAioListener`
50-
- `getServerListener`
41+
3. **`httpConfig`**: 类似于`wsServerConfig` 代表 HTTP 服务配置。包括最大头部大小、会话超时时间等设置。
5142

52-
## 常用类解释
43+
4. **`defaultHttpRequestHandlerDispather`**: 这是处理 HTTP 请求的默认分发器。它定义了如何处理和路由进来的 HTTP 请求。tio-boot 启动时会设置为 com.litongjava.tio.boot.http.handler.DefaultHttpRequestHandlerDispather
44+
45+
5. **`defaultHttpServerInterceptorDispatcher`**: 这代表 HTTP 拦截器的分发器,可以拦截并处理 HTTP 请求和响应,tio-boot 启动时会设置为 com.litongjava.tio.boot.http.interceptor.DefaultHttpServerInterceptorDispatcher
46+
47+
6. **`defaultWebSocketHandlerDispather`**: 这代表 websocket 的分发器,它定义了如何处理进来的 WebSocket 消息。tio-boot 启动时会设置为 com.litongjava.tio.boot.websocket.handler.DefaultWebSocketHandlerDispather
48+
49+
7. **`serverInteceptorConfigure`**: 这是服务器拦截器的配置设置,允许你为所有进来的请求或消息定义全局规则或行为。需要配合 DefaultHttpRequestHandlerDispather 使用
50+
51+
8. **`webSocketRoutes`**: 定义 WebSocket 连接的路由,每个路由都与不同的行为或处理逻辑相关联。需要配合 DefaultWebSocketHandlerDispather 使用
52+
53+
9. **`tioBootServerListener`**: 这个监听器可能处理与服务器生命周期相关的事件,比如服务器启动、停止或遇到错误时。
54+
55+
10. **`tioBootHttpRoutes`**: 管理 tio-boot http controller 的路由。每个路由指定了不同 HTTP 路径应该如何处理。
56+
57+
11. **`httpRoutes`**: 管理 tio http server hanlder 的路由
58+
59+
12. **`serverTcpHandler`**: 处理 TCP 连接。它负责处理通过 TCP 连接接收到的数据,并可能发送响应。
60+
61+
13. **`serverAioListener`**: 请求 的监听器。这如接受连接、断开连接等.
62+
63+
## 设置和获取配置
5364

54-
- `ServerInteceptorConfigure` 管理拦截器
55-
- `TioBootHttpRoutes` 管理 tio-boot controller
56-
- `HttpRoutes` 管理 tio-http-server handler
57-
- `ServerTcpHandler` 管理 tcpHnalder
58-
- `ServerAioListener` tcp 请求监听器
59-
- `ServerListener` tio-boot server 监听器
65+
上面这些熟悉都提供了静态的 get 和 set 方法,你可以在需要的时候获取配置或者设置自定义的配置

docs/zh/06_内置组件/04.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public class IntecpetorConfiguration {
146146

147147
@AInitialization
148148
public void serverInteceptorRoutes() {
149-
ServerInteceptorConfigure config = new ServerInteceptorConfigure();
149+
150150
// add global
151151
HttpServerInterceptorModel global = new HttpServerInterceptorModel();
152152
global.setName("global");
@@ -158,6 +158,7 @@ public class IntecpetorConfiguration {
158158
hello.addblockeUrl("/hello");
159159
hello.setInterceptor(new HelloInteceptor());
160160

161+
ServerInteceptorConfigure config = new ServerInteceptorConfigure();
161162
config.add(global);
162163
config.add(hello);
163164

@@ -179,4 +180,4 @@ public class IntecpetorConfiguration {
179180
- `doBeforeHandler` 方法在处理 HTTP 请求之前执行。对于 `GlobalInteceptor`,它返回 `responseFromCache`,而 `HelloInteceptor` 返回 `null`
180181
- `doAfterHandler` 方法在请求处理后执行,记录请求信息。
181182

182-
3. DefaultHttpServerInterceptor 用于执行拦截器,拦截器的执行顺序是 config.add 添加的顺序
183+
3. DefaultHttpServerInterceptor 用于执行拦截器,拦截器的执行顺序是 config.add 添加的顺序

0 commit comments

Comments
 (0)