Skip to content

Commit 67fd024

Browse files
author
litongjava
committed
add close response
1 parent 06c7749 commit 67fd024

File tree

2 files changed

+61
-53
lines changed

2 files changed

+61
-53
lines changed

docs/zh/05_web开发/02.md

+57-52
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# JSON
22

3-
#### 6.2.1.实体类
3+
### 实体类
44

55
```java
66
package top.ppnt.java.ee.tio.http.server.boot.model;
@@ -22,19 +22,19 @@ public class User {
2222
}
2323
```
2424

25-
#### 6.2.2.接收 Json 数据
25+
### 接收 Json 数据
2626

27-
##### 从 http 请求中获取 json 字符串
27+
#### 从 http 请求中获取 json 字符串
2828

2929
从 http 请求中获取 json 字符串,接收 JSON 数据有需要手动转为 JavaBean
3030

31-
```
31+
```java
3232
String bodyString = request.getBodyString();
3333
```
3434

3535
示例代码
3636

37-
```
37+
```java
3838
package com.litongjava.tio.boot.hello.AController;
3939

4040
import com.litongjava.tio.http.common.HttpRequest;
@@ -85,10 +85,12 @@ curl --location --request POST 'http://127.0.0.1/test/json/getBodyString' \
8585
}'
8686
```
8787

88-
##### 自动封装为 java bean
88+
#### 自动封装为 java bean
8989

9090
在 Action 的方法签名上添加参数 User user
9191

92+
##### 示例代码
93+
9294
```
9395
import com.litongjava.tio.boot.hello.model.User;
9496
import com.litongjava.tio.http.common.HttpRequest;
@@ -111,16 +113,14 @@ public class TestJsonController {
111113
112114
```
113115

114-
支持 application/json
116+
#### 自动封装为 java bean 支持下面这些格式的请求
115117

116-
```
118+
##### 支持 POST 请求格式 application/json
119+
120+
```http
117121
POST /demo/bean HTTP/1.1
118122
Host: 127.0.0.1
119-
User-Agent: apifox/1.0.0 (https://www.apifox.cn)
120123
Content-Type: application/json
121-
Accept: _/_
122-
Host: 127.0.0.1
123-
Connection: keep-alive
124124
125125
{
126126
"ip": "69.134.20.34",
@@ -129,56 +129,50 @@ Connection: keep-alive
129129
}
130130
```
131131

132-
支持 application/x-www-form-urlencoded
132+
##### 支持 POST 请求格式 application/x-www-form-urlencoded
133+
134+
```http
133135
134-
```
135136
GET /demo/bean HTTP/1.1
136137
Host: 127.0.0.1
137-
User-Agent: apifox/1.0.0 (https://www.apifox.cn)
138-
Accept: _/_
139-
Host: 127.0.0.1
140-
Connection: keep-alive
141138
Content-Type: application/x-www-form-urlencoded
142139
143140
loginName=Ping%20E%20Lee&nick=%E6%9D%8E%E9%80%9A&ip=127.0.0..1
141+
144142
```
145143

146-
支持发送 POST 请求 multipart/form-data
144+
###### 支持 POST 请求格式 multipart/form-data
145+
147146
示例
148147

149-
```
150-
GET /demo/bean HTTP/1.1
151-
Host: 127.0.0.1
152-
User-Agent: apifox/1.0.0 (https://www.apifox.cn)
153-
Accept: _/_
148+
```http
149+
POST /demo/bean HTTP/1.1
154150
Host: 127.0.0.1
155-
Connection: keep-alive
156151
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
157152
158153
----WebKitFormBoundary7MA4YWxkTrZu0gW
159154
----WebKitFormBoundary7MA4YWxkTrZu0gW
160155
----WebKitFormBoundary7MA4YWxkTrZu0gW
161156
----WebKitFormBoundary7MA4YWxkTrZu0gW
157+
162158
```
163159

164-
支持 Get 请求
160+
##### 支持 Get 请求 Url 传递参数
161+
165162
示例如下
166163

167-
```
164+
```http
165+
168166
GET /demo/bean?loginName=Ping%20E%20Lee&nick=%E6%9D%8E%E9%80%9A&ip=127.0.0..1 HTTP/1.1
169167
Host: 127.0.0.1
170-
User-Agent: apifox/1.0.0 (https://www.apifox.cn)
171-
Accept: _/_
172-
Host: 127.0.0.1
173-
Connection: keep-alive
174168
```
175169

176-
#### 6.2.3.返回 Json 数据
170+
### 返回 Json 数据
177171

178172
直接返回 json 数据
179173
Action 的返回值可以直接是实体类,框架会自动进行转换
180174

181-
```
175+
```java
182176
package com.litongjava.tio.boot.hello.AController;
183177

184178
import com.litongjava.tio.boot.hello.model.User;
@@ -197,7 +191,6 @@ public class TestJsonController {
197191
public User responseUser(HttpRequest request) throws Exception {
198192
return User.builder().loginName("Ping E Lee").nick("李通").ip("127.0.0.1").build();
199193
}
200-
201194
}
202195

203196
```
@@ -207,17 +200,18 @@ public class TestJsonController {
207200
该方法无需额外的 HTTP 响应处理,因为 tio-boot 框架将自动处理 User 对象的序列化并返回 JSON 格式的响应。
208201
响应
209202

210-
```
203+
```json
211204
{
212-
"ip": "127.0.0.1",
213-
"loginName": "Ping E Lee",
214-
"nick": "李通"
205+
"ip": "127.0.0.1",
206+
"loginName": "Ping E Lee",
207+
"nick": "李通"
215208
}
216209
```
217210

218211
使用 Resps.json(request, user);返回 json 数据
219212

220-
```
213+
```java
214+
221215
package com.litongjava.tio.boot.hello.AController;
222216

223217
import com.litongjava.tio.boot.hello.model.User;
@@ -239,6 +233,7 @@ public class TestJsonController {
239233
return ret;
240234
}
241235
}
236+
242237
```
243238

244239
**Method `responseJson`**:
@@ -252,22 +247,25 @@ public class TestJsonController {
252247
http://127.0.0.1/demo/responseJson
253248
响应
254249

255-
```
250+
```json
256251
{
257-
"ip": "127.0.0.1",
258-
"loginName": "Ping E Lee",
259-
"nick": "李通"
252+
"ip": "127.0.0.1",
253+
"loginName": "Ping E Lee",
254+
"nick": "李通"
260255
}
261256
```
262257

263258
使用 Resp 返回 Json 数据
264259
核心代码
265260

266261
```
262+
267263
return Resps.json(request, Resp.ok(systemInfo));
264+
268265
```
269266

270267
```
268+
271269
package com.litongjava.tio.boot.hello.AController;
272270
273271
import com.litongjava.tio.boot.hello.model.User;
@@ -289,6 +287,7 @@ public class TestJsonController {
289287
return Resps.json(request, Resp.ok(user));
290288
}
291289
}
290+
292291
```
293292

294293
**Method `responseJsonResp`**:
@@ -299,19 +298,24 @@ public class TestJsonController {
299298
- 最后,方法返回这个 `HttpResponse` 对象。
300299
返回数据如下
301300

302-
```
301+
```json
303302
{
304-
"data": "数据部分",
305-
"ok": true
303+
"data": "数据部分",
304+
"ok": true
306305
}
307306
```
308307

309-
使用 RespVo 返回 Json 数据
308+
##### 使用 RespVo 返回 Json 数据
309+
310310
核心代码
311+
312+
```
311313
return Resps.json(request, RespVo.ok(data));
314+
```
315+
312316
示例
313317

314-
```
318+
```java
315319
package com.litongjava.tio.boot.hello.AController;
316320

317321
import com.litongjava.tio.boot.hello.model.User;
@@ -334,6 +338,7 @@ public class TestJsonController {
334338
return Resps.json(request, RespVo.ok(user));
335339
}
336340
}
341+
337342
```
338343

339344
**Method `responseJsonResps`**:
@@ -344,9 +349,9 @@ public class TestJsonController {
344349
- 最后,方法返回这个 `HttpResponse` 对象。
345350
返回数据如下
346351

347-
```
352+
```json
348353
{
349-
"data": "数据部分",
350-
"ok": true
354+
"data": "数据部分",
355+
"ok": true
351356
}
352-
```
357+
```

docs/zh/99_案例/02.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ import com.litongjava.tio.utils.environment.EnvironmentUtils;
145145
import com.litongjava.tio.utils.network.IpUtils;
146146
import com.litongjava.tio.utils.notification.NotifactionWarmModel;
147147
import com.litongjava.tio.utils.notification.NotificationUtils;
148+
import okhttp3.Response;
148149
149150
import lombok.extern.slf4j.Slf4j;
150151
@@ -176,7 +177,9 @@ public class GlobalExceptionHadler implements TioBootExceptionHandler {
176177
model.setTime(new Date());
177178
model.setContent(requestURL + "\n" + stackTrace);
178179
179-
NotificationUtils.sendWarm(model);
180+
Response response = NotificationUtils.sendWarm(model);
181+
//close response
182+
response.close();
180183
181184
}
182185
}

0 commit comments

Comments
 (0)