Spring WebFlux使用
# demo
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.reactive.function.client.WebClient;
@SpringBootApplication
public class WebClientDemoApplication {
public static void main(String[] args) {
SpringApplication.run(WebClientDemoApplication.class, args);
// 创建一个WebClient实例
WebClient webClient = WebClient.create();
// 发起GET请求并获取响应
webClient.get()
.uri("https://jsonplaceholder.typicode.com/users/1")
.retrieve()
.bodyToMono(String.class)
.subscribe(response -> {
System.out.println("Response: " + response);
}, error -> {
System.err.println("Error: " + error.getMessage());
});
}
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 测试
# 流模拟
return Flux.range(1, 100000)
.map(n -> "Number: " + n);
1
2
3
2
3
# sse和flux测试
1
# 参考文档
上次更新: 2023-06-26 10:51:10