-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMvcController.java
300 lines (269 loc) · 8.49 KB
/
MvcController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package com.example;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.IOException;
import java.io.Serializable;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
@SuppressWarnings("JavadocLinkAsPlainText")
@RestController
public class MvcController {
/**
* 无参数
* http://localhost:8080/base
*/
@GetMapping("/base")
public String base() {
return "base";
}
/**
* 无注解,使用参数
* http://localhost:8080/entry?key=k&value=v
*/
@GetMapping("/entry")
public String entry(Entry entry) {
return "entry: " + entry.getKey() + "/" + entry.getValue();
}
/**
* 无注解,使用路径参数
* http://localhost:8080/entry/k/v
*/
@GetMapping("/entry/{key}/{value}")
public String entry1(Entry entry) {
return "entry: " + entry.getKey() + "/" + entry.getValue();
}
@Data
public static class Entry implements Serializable {
private String key;
private String value;
}
/**
* 使用参数
* http://localhost:8080/param?param=param
*/
@GetMapping("/param")
public String param(@RequestParam("param") String param) {
return "param: " + param;
}
/**
* 可选,使用{@link Optional}代替{@link RequestParam#required()}
* http://localhost:8080/param1?param1=param1
* http://localhost:8080/param1
*/
@GetMapping("/param1")
public String param1(@RequestParam("param1") Optional<String> optional) {
return optional.map(param -> "param1: " + param).orElse("param1");
}
/**
* 路径参数
* http://localhost:8080/uriPattern/param
*/
@GetMapping("/uriPattern/{param}")
public String uriPattern(@PathVariable("param") String param) {
return "uriPattern: " + param;
}
/**
* 路径参数可用正则
* http://localhost:8080/uriRegex/param
*/
@GetMapping("/uriRegex/{param:[a-zA-Z]*}")
public String uriRegex(@PathVariable("param") String param) {
return "uriRegex: " + param;
}
/**
* http://localhost:8080/uriRegex/123
*/
@GetMapping("/uriRegex/{param:[0-9]*}")
public String uriRegex1(@PathVariable("param") Integer param) {
return "uriRegex1: " + param;
}
/**
* 矩阵
* http://localhost:8080/matrix/path;a=a;b=b
*/
@GetMapping("/matrix/{path}")
public String matrix(@MatrixVariable("a") String a, @MatrixVariable("b") String b) {
return "matrix: a=" + a + ", b=" + b;
}
/**
* http://localhost:8080/matrix1/path;a=a;b=b/path1;a=a1;b=b1
*/
@GetMapping("/matrix1/{path}/{path1}")
public String matrix1(@MatrixVariable(value = "a", pathVar = "path") String a,
@MatrixVariable(value = "a", pathVar = "path1") String a1,
@MatrixVariable(value = "b", pathVar = "path") String b,
@MatrixVariable(value = "b", pathVar = "path1") String b1) {
return "matrix: a=" + a + ", a1=" + a1 + ", b=" + b + ", b1=" + b1;
}
/**
* http://localhost:8080/matrix2/path;a=a;b=b/path1;a=a1;b=b1
*/
@GetMapping("/matrix2/{path}/{path1}")
public String matrix2(@MatrixVariable MultiValueMap<String, String> map) {
// matrix: {a=[a, a1], b=[b, b1]}
return "matrix: " + map;
}
/**
* http头
* http://localhost:8080/head
*/
@GetMapping("/head")
public String head(@RequestHeader("myHead") String host) {
return "head[myHead]: " + host;
}
/**
* cookie
* http://localhost:8080/cookie
*/
@GetMapping("/cookie")
public String cookie(@CookieValue("cookie") String cookie) {
return "cookie[cookie]: " + cookie;
}
/**
* {@link HttpServletRequest}原始请求
* http://localhost:8080/request
*/
@GetMapping("/request")
public String request(HttpServletRequest request) {
return "request: " + request.getMethod();
}
/**
* {@link HttpServletResponse}原始响应
* http://localhost:8080/response
*/
@GetMapping("/response")
public void response(HttpServletResponse response) throws IOException {
ServletOutputStream outputStream = response.getOutputStream();
outputStream.print("response");
outputStream.flush();
}
/**
* 返回json
* 注意: 需要{@link ResponseBody}或在类上使用{@link RestController}
* http://localhost:8080/retEntry
*/
@GetMapping("/retEntry")
public Entry retEntry() {
Entry entry = new Entry();
entry.setKey("key");
entry.setValue("value");
return entry;
}
/**
* 长连接
* http://localhost:8080/deferred
*/
@GetMapping("/deferred")
public DeferredResult<String> deferred() {
DeferredResult<String> result = new DeferredResult<>();
AtomicLong ret = new AtomicLong();
Thread.ofVirtual().start(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
result.setResult(Long.toString(System.currentTimeMillis() - ret.get()));
}
});
ret.set(System.currentTimeMillis());
return result;
}
/**
* 分多次输出(一次返回)
* http://localhost:8080/stream
*/
@GetMapping("/stream")
public ResponseBodyEmitter stream() {
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
Thread.ofVirtual().start(() -> {
try {
for (int i = 0; i < 30; i++) {
emitter.send("send[" + i + "]: " + System.currentTimeMillis() + "\n");
TimeUnit.MILLISECONDS.sleep(50);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
emitter.complete();
}
});
return emitter;
}
/**
* 分多次输出(多次返回)
* http://localhost:8080/sse
*/
@GetMapping("/sse")
public SseEmitter sse() {
SseEmitter emitter = new SseEmitter();
Thread.ofVirtual().start(() -> {
try {
for (int i = 0; i < 30; i++) {
emitter.send("send[" + i + "]: " + System.currentTimeMillis());
TimeUnit.MILLISECONDS.sleep(50);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
emitter.complete();
}
});
return emitter;
}
/**
*
*/
@PostMapping("/body")
public String body(@RequestBody String body) {
return "body: " + body;
}
/**
* 表单处理
* http://localhost:8080/form
*/
@PostMapping("/form")
public String form(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) throws IOException {
return "form[" + name + "]: " + new String(file.getBytes());
}
/**
* 表单处理
* http://localhost:8080/form1
*/
@PostMapping("/form1")
public String form1(MyForm form) throws IOException {
return "form1[" + form.name + "]: " + new String(form.file.getBytes());
}
@Data
public static class MyForm implements Serializable {
private String name;
private MultipartFile file;
}
/**
* 异常处理{@link #error(MyException)}
* http://localhost:8080/exception
*/
@GetMapping("/exception")
public String exception() {
throw new MyException("ERROR");
}
@ExceptionHandler(MyException.class)
public ResponseEntity<String> error(MyException e) {
return ResponseEntity.internalServerError().body(e.getMessage());
}
public static class MyException extends RuntimeException {
public MyException(String error) {
super(error);
}
}
}