您现在的位置是:首页 > 技术教程 正文

WebClient 同步、异步调用实现对比

admin 阅读: 2024-03-31
后台-插件-广告管理-内容页头部广告(手机)

文章目录

  • 一、概述
  • 二、pom依赖
  • 三、代码结构
  • 四、源码传送
    • 1、异步代码
    • 2、同步代码
    • 3、完整代码

一、概述

WebClient是Spring WebFlux模块提供的一个非阻塞的基于响应式编程的进行Http请求的客户端工具,从Spring5.0开始WebClient作为RestTemplete的替代品,有更好的响应式能力,支持异步调用,可以在Spring项目中实现网络请求。

二、pom依赖

<dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webfluxartifactId> <version>5.2.3.RELEASEversion> dependency> <dependency> <groupId>io.projectreactor.nettygroupId> <artifactId>reactor-nettyartifactId> <version>0.9.4.RELEASEversion> dependency> <dependency> <groupId>org.apache.logging.log4jgroupId> <artifactId>log4j-slf4j-implartifactId> <version>2.12.1version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>2.13.0version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-lang3artifactId> <version>3.10version> dependency> <dependency> <groupId>commons-iogroupId> <artifactId>commons-ioartifactId> <version>2.5version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>1.18.12version> <scope>providedscope> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> dependency>
  • 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

三、代码结构

在这里插入图片描述

图片请手工放入 src\test\resources\123.jpg
在这里插入图片描述
单元测试
在这里插入图片描述

四、源码传送

1、异步代码

import java.awt.Desktop; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import org.apache.commons.lang3.RandomUtils; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.util.FileCopyUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.util.UriComponentsBuilder; import com.fly.http.bean.ImageShowDialog; import com.fly.http.bean.JsonBeanUtils; import com.fly.http.bean.SearchReq; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Mono; /** * http请求WebClient异步调用实现 */ @Slf4j public class WebClientAsyncTest { private WebClient webClient = WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build(); private void openImage(Resource resource) { try { new ImageShowDialog(ImageIO.read(resource.getInputStream())); } catch (IOException e) { log.error(e.getMessage(), e); } } @BeforeClass public static void init() { new File("download").mkdirs(); } /** * WebClient异步调用 * * @throws IOException * @throws InterruptedException */ @Test public void testDownFile() throws IOException, InterruptedException { Mono<Resource> mono = webClient.get() .uri("https://00fly.online/upload/urls.txt") .accept(MediaType.IMAGE_JPEG) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(Resource.class)); // 保存到本地 mono.subscribe(resource -> { try { FileCopyUtils.copy(resource.getInputStream(), new FileOutputStream("download/urls.txt")); } catch (IOException e) { log.error(e.getMessage(), e); } }); TimeUnit.SECONDS.sleep(2); } /** * WebClient异步调用 * * @throws IOException * @throws InterruptedException */ @Test public void testDownImg001() throws IOException, InterruptedException { Mono<Resource> mono = webClient.get() .uri("https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg") .accept(MediaType.IMAGE_JPEG) .retrieve() // 获取响应体 .bodyToMono(Resource.class); mono.subscribe(resource -> openImage(resource)); TimeUnit.SECONDS.sleep(10); } /** * WebClient异步调用 * * @throws IOException * @throws InterruptedException */ @Test public void testDownImg002() throws IOException, InterruptedException { Mono<Resource> mono = webClient.get() .uri("https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg") .accept(MediaType.IMAGE_JPEG) .retrieve() // 获取响应体 .bodyToMono(Resource.class); // 保存到本地 mono.subscribe(resource -> { try { File dest = new File(String.format("download/img_%s.jpg", System.currentTimeMillis())); FileCopyUtils.copy(resource.getInputStream(), new FileOutputStream(dest)); if (Desktop.isDesktopSupported()) { Desktop.getDesktop().open(dest.getParentFile()); } } catch (IOException e) { log.error(e.getMessage(), e); } }); TimeUnit.SECONDS.sleep(2); } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testExchange001() throws InterruptedException { // get MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<ClientResponse> monoGet = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https") .host("httpbin.org") .path("/get") .queryParams(params) // 等价 queryParam("q1", "java").queryParam("q2", "python") .build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange(); monoGet.subscribe(clientResponse -> { log.info("----- headers: {}", clientResponse.headers()); log.info("----- statusCode: {}", clientResponse.statusCode()); clientResponse.bodyToMono(String.class).subscribe(body -> log.info("----- reponse: {}", body)); }); TimeUnit.SECONDS.sleep(2); } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testExchange002() throws InterruptedException { // get Mono<ClientResponse> monoGet = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/get").queryParam("q1", "java").queryParam("q2", "python").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange(); monoGet.subscribe(clientResponse -> { log.info("----- headers: {}", clientResponse.headers()); log.info("----- statusCode: {}", clientResponse.statusCode()); clientResponse.bodyToMono(String.class).subscribe(body -> log.info("----- reponse: {}", body)); }); // formData post MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<ClientResponse> monoPost = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromFormData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .exchange(); monoPost.subscribe(clientResponse -> { log.info("----- headers: {}", clientResponse.headers()); log.info("----- statusCode: {}", clientResponse.statusCode()); clientResponse.bodyToMono(String.class).subscribe(body -> log.info("----- reponse: {}", body)); }); TimeUnit.SECONDS.sleep(2); } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testFormDataPost() throws InterruptedException { MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromFormData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testGet001() throws InterruptedException { Mono<String> mono = webClient.get() .uri("https://httpbin.org/{method}", "get") // {任意命名} .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testGet002() throws InterruptedException { Mono<ClientResponse> mono = webClient.get().uri("https://httpbin.org/get").acceptCharset(StandardCharsets.UTF_8).accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML).exchange(); mono.subscribe(reponse -> { log.info("----- headers: {}", reponse.headers()); log.info("----- statusCode: {}", reponse.statusCode()); reponse.bodyToMono(String.class).subscribe(body -> log.info("----- reponse: {}", body)); }); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testGet003() throws InterruptedException { Mono<String> mono = webClient.get() .uri("https://httpbin.org/get") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用, https://httpbin.org/get?q=java * * @throws InterruptedException */ @Test public void testGet004() throws InterruptedException { MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q", "java"); String uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParams(params).toUriString(); // 注意比较 // uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParam("q", "java", "python").toUriString(); // uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParam("q1", "java").queryParam("q2", "python").toUriString(); Mono<String> mono = webClient.get() .uri(uri) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testGet005() throws InterruptedException { Mono<String> mono = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/get").queryParam("q1", "java").queryParam("q2", "python").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testJsonBody001() throws InterruptedException { Mono<String> mono = webClient.post() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/post").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .contentType(MediaType.APPLICATION_JSON) .bodyValue(Collections.singletonMap("q", "java")) .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws IOException * @throws InterruptedException */ @Test public void testJsonBody002() throws IOException, InterruptedException { Mono<String> mono; int num = RandomUtils.nextInt(1, 4); switch (num) { case 1: // 方式1,javaBean SearchReq req = new SearchReq(); req.setPageNo(1); req.setPageSize(10); req.setKeyword("1"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .bodyValue(req) // 设置JsonBody .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("reponse: {}", body)); break; case 2: // 方式2,HashMap Map<String, String> params = new HashMap<>(); params.put("pageNo", "2"); params.put("pageSize", "20"); params.put("keyword", "2"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .bodyValue(params) // 设置JsonBody .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("reponse: {}", body)); break; case 3: // 方式3,json字符串 Map<String, String> params2 = new HashMap<>(); params2.put("pageNo", "2"); params2.put("pageSize", "20"); params2.put("keyword", "2"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromValue(JsonBeanUtils.beanToJson(params2, false))) // 设置formData .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("reponse: {}", body)); break; } TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testUpload001() throws InterruptedException { MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); params.add("file", new ClassPathResource("123.jpg")); Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromMultipartData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testUpload002() throws InterruptedException { Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromMultipartData("q1", "java").with("q2", "python").with("file", new ClassPathResource("123.jpg"))) .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } }
  • 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
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474

2、同步代码

import java.awt.Desktop; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import org.apache.commons.lang3.RandomUtils; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.util.FileCopyUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.util.UriComponentsBuilder; import com.fly.http.bean.ImageShowDialog; import com.fly.http.bean.JsonBeanUtils; import com.fly.http.bean.SearchReq; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Mono; /** * http请求WebClient同步调用实现 */ @Slf4j public class WebClientSyncTest { private WebClient webClient = WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build(); private void openImage(Resource resource) { try { new ImageShowDialog(ImageIO.read(resource.getInputStream())); } catch (IOException e) { log.error(e.getMessage(), e); } } @BeforeClass public static void init() { new File("download").mkdirs(); } /** * WebClient同步调用 * * @throws IOException */ @Test public void testDownFile() throws IOException { Mono<ClientResponse> mono = webClient.get().uri("https://00fly.online/upload/urls.txt").accept(MediaType.IMAGE_JPEG).exchange(); ClientResponse response = mono.block(); log.info("----- headers: {}", response.headers()); log.info("----- statusCode: {}", response.statusCode()); // 保存到本地 Resource resource = response.bodyToMono(Resource.class).block(); FileCopyUtils.copy(resource.getInputStream(), new FileOutputStream("download/urls.txt")); } /** * WebClient同步调用 * * @throws IOException * @throws InterruptedException */ @Test public void testDownImg001() throws IOException, InterruptedException { Mono<Resource> mono = webClient.get() .uri("https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg") .accept(MediaType.IMAGE_JPEG) .retrieve() // 获取响应体 .bodyToMono(Resource.class); openImage(mono.block()); TimeUnit.SECONDS.sleep(10); } /** * WebClient同步调用 * * @throws IOException */ @Test public void testDownImg002() throws IOException { Mono<Resource> mono = webClient.get() .uri("https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg") .accept(MediaType.IMAGE_JPEG) .retrieve() // 获取响应体 .bodyToMono(Resource.class); // 保存到本地 Resource resource = mono.block(); File dest = new File(String.format("download/img_%s.jpg", System.currentTimeMillis())); FileCopyUtils.copy(resource.getInputStream(), new FileOutputStream(dest)); if (Desktop.isDesktopSupported()) { Desktop.getDesktop().open(dest.getParentFile()); } } /** * WebClient同步调用 */ @Test public void testExchange001() { // get MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<ClientResponse> monoGet = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https") .host("httpbin.org") .path("/get") .queryParams(params) // 等价 queryParam("q1", "java").queryParam("q2", "python") .build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange(); ClientResponse clientResponse = monoGet.block(); // 获取完整的响应对象 log.info("----- headers: {}", clientResponse.headers()); log.info("----- statusCode: {}", clientResponse.statusCode()); log.info("----- reponse: {}", clientResponse.bodyToMono(String.class).block()); } /** * WebClient同步调用 */ @Test public void testExchange002() { // get Mono<ClientResponse> monoGet = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/get").queryParam("q1", "java").queryParam("q2", "python").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange(); ClientResponse clientResponse = monoGet.block(); // 获取完整的响应对象 log.info("----- headers: {}", clientResponse.headers()); log.info("----- statusCode: {}", clientResponse.statusCode()); log.info("----- reponse: {}", clientResponse.bodyToMono(String.class).block()); // formData post MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<ClientResponse> monoPost = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromFormData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .exchange(); ClientResponse clientResponse2 = monoPost.block(); // 获取完整的响应对象 log.info("----- headers: {}", clientResponse2.headers()); log.info("----- statusCode: {}", clientResponse2.statusCode()); log.info("----- reponse: {}", clientResponse2.bodyToMono(String.class).block()); } /** * WebClient同步调用 */ @Test public void testFormDataPost() { MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromFormData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 */ @Test public void testGet001() { Mono<String> mono = webClient.get() .uri("https://httpbin.org/{method}", "get") // {任意命名} .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 */ @Test public void testGet002() { Mono<String> mono = webClient.get() .uri("https://httpbin.org/get") .acceptCharset(StandardCharsets.UTF_8) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 * */ @Test public void testGet003() { Mono<String> mono = webClient.get() .uri("https://httpbin.org/get") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用, https://httpbin.org/get?q=java * */ @Test public void testGet004() { MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q", "java"); String uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParams(params).toUriString(); // 注意比较 // uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParam("q", "java", "python").toUriString(); // uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParam("q1", "java").queryParam("q2", "python").toUriString(); Mono<String> mono = webClient.get() .uri(uri) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 * */ @Test public void testGet005() { Mono<String> mono = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/get").queryParam("q1", "java").queryParam("q2", "python").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 * */ @Test public void testJsonBody001() { Mono<String> mono = webClient.post() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/post").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .contentType(MediaType.APPLICATION_JSON) .bodyValue(Collections.singletonMap("q", "java")) .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 * * @throws IOException */ @Test public void testJsonBody002() throws IOException { Mono<String> mono; int num = RandomUtils.nextInt(1, 4); switch (num) { case 1: // 方式1,javaBean SearchReq req = new SearchReq(); req.setPageNo(1); req.setPageSize(10); req.setKeyword("1"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .bodyValue(req) // 设置JsonBody .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); break; case 2: // 方式2,HashMap Map<String, String> params = new HashMap<>(); params.put("pageNo", "2"); params.put("pageSize", "20"); params.put("keyword", "2"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .bodyValue(params) // 设置JsonBody .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); break; case 3: // 方式3,json字符串 Map<String, String> params2 = new HashMap<>(); params2.put("pageNo", "2"); params2.put("pageSize", "20"); params2.put("keyword", "2"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromValue(JsonBeanUtils.beanToJson(params2, false))) // 设置formData .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); break; } } /** * WebClient同步调用 * */ @Test public void testUpload001() { MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); params.add("file", new ClassPathResource("123.jpg")); Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromMultipartData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 log.info("----- reponse: {}", mono.block()); } /** * WebClient同步调用 * */ @Test public void testUpload002() { Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromMultipartData("q1", "java").with("q2", "python").with("file", new ClassPathResource("123.jpg"))) .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 log.info("----- reponse: {}", mono.block()); } }
  • 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
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411

3、完整代码

如何使用下面的备份文件恢复成原始的项目代码,请移步查阅:神奇代码恢复工具

//goto docker\docker-compose.yml version: '3.7' services: hello: image: registry.cn-shanghai.aliyuncs.com/00fly/web-client:0.0.1 container_name: web-client deploy: resources: limits: cpus: '1' memory: 200M reservations: cpus: '0.05' memory: 100M environment: JAVA_OPTS: -server -Xms100m -Xmx100m -Djava.security.egd=file:/dev/./urandom restart: on-failure logging: driver: json-file options: max-size: 5m max-file: '1' //goto docker\restart.sh #!/bin/bash docker-compose down && docker system prune -f && docker-compose up -d && docker stats //goto docker\stop.sh #!/bin/bash docker-compose down //goto Dockerfile FROM openjdk:8-jre-alpine RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone COPY target/web-client-*.jar /app.jar ENTRYPOINT ["java","-jar","/app.jar"] //goto pom.xml <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.fly</groupId> <artifactId>web-client</artifactId> <version>0.0.1</version> <name>web-client</name> <packaging>jar</packaging> <properties> <docker.hub>registry.cn-shanghai.aliyuncs.com</docker.hub> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <skipTests>true</skipTests> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webflux</artifactId> <version>5.2.3.RELEASE</version> </dependency> <dependency> <groupId>io.projectreactor.netty</groupId> <artifactId>reactor-netty</artifactId> <version>0.9.4.RELEASE</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.12.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.10</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}-${project.version}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.0</version> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <minimizeJar>false</minimizeJar> <filters> <filter> <artifact>*:*</artifact> </filter> </filters> <transformers> <!--MANIFEST文件中写入Main-Class是可执行包的必要条件。ManifestResourceTransformer可以轻松实现。 --> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.fly.http.RunMain</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> <!-- 添加docker-maven插件 --> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.40.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>build</goal> <goal>push</goal> <goal>remove</goal> </goals> </execution> </executions> <configuration> <!-- 连接到带docker环境的linux服务器编译image --> <!-- <dockerHost>http://192.168.182.10:2375</dockerHost> --> <!-- Docker 推送镜像仓库地址 --> <pushRegistry>${docker.hub}</pushRegistry> <images> <image> <name> ${docker.hub}/00fly/${project.artifactId}:${project.version}</name> <build> <dockerFileDir>${project.basedir}</dockerFileDir> </build> </image> </images> </configuration> </plugin> </plugins> </build> </project> //goto src\main\java\com\fly\http\FluxWebClient.java package com.fly.http; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.http.MediaType; import org.springframework.web.reactive.function.client.WebClient; import lombok.extern.slf4j.Slf4j; /** * WebClient是RestTemplete的替代品,有更好的响应式能力,支持异步调用
* * https://blog.csdn.net/zzhongcy/article/details/105412842 * */
@Slf4j public class FluxWebClient { private List<String> urls = new ArrayList<>(); // 缓冲区默认256k,设为-1以解决报错Exceeded limit on max bytes to buffer : 262144 private WebClient webClient = WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build(); public void visitAll() { // block转换为同步调用 if (urls.isEmpty()) { log.info("★★★★★★★★ urls isEmpty, now get urls from api ★★★★★★★★"); String resp = webClient.get().uri("https://00fly.online/upload/urls.txt").acceptCharset(StandardCharsets.UTF_8).accept(MediaType.TEXT_HTML).retrieve().bodyToMono(String.class).block(); urls = Arrays.asList(StringUtils.split(resp, "\r\n")); } // 异步访问 AtomicInteger count = new AtomicInteger(0); urls.stream() .filter(url -> RandomUtils.nextBoolean()) .forEach(url -> webClient.get() .uri(url) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.TEXT_HTML) .retrieve() .bodyToMono(String.class) .subscribe(r -> log.info("process complted: {}. {}", count.incrementAndGet(), url), e -> log.error(e.getMessage()))); log.info("total:{} ==> ############## 异步请求已提交 ##############", urls.size()); } } //goto src\main\java\com\fly\http\RunMain.java package com.fly.http; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; @Slf4j public class RunMain { private static FluxWebClient webClient = new FluxWebClient(); /** * 程序运行入口 * */ public static void main(String[] args) { scheduledThreadPoolExecutorStart(); } private static void scheduledThreadPoolExecutorStart() { new ScheduledThreadPoolExecutor(2).scheduleAtFixedRate(() -> { webClient.visitAll(); }, 0L, 30, TimeUnit.SECONDS); log.info("======== ScheduledThreadPoolExecutor started!"); } /** * Timer线程安全, 但单线程执行, 抛出异常时, task会终止 */ @Deprecated protected static void timeStart() { new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { webClient.visitAll(); } }, 0L, 30 * 1000L); log.info("======== Timer started!"); } } //goto src\main\resources\log4j2.xml <?xml version="1.0" encoding="UTF-8"?> <configuration status="off" monitorInterval="0"> <appenders> <console name="Console" target="system_out"> <patternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p %pid --- [%t] %-30.30c{1.} : %m%n" /> </console> </appenders> <loggers> <root level="INFO"> <appender-ref ref="Console" /> </root> </loggers> </configuration> //goto src\test\java\com\fly\http\ApiTest.java package com.fly.http; import java.awt.Desktop; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.junit.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.util.ResourceUtils; import org.springframework.web.reactive.function.client.WebClient; import lombok.extern.slf4j.Slf4j; @Slf4j public class ApiTest { // 缓冲区默认256k,设为-1以解决报错Exceeded limit on max bytes to buffer : 262144 private WebClient webClient = WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build(); /** * 写入文本文件 * * @param urls * @see [类、类#方法、类#成员] */ private void process(List<String> urls) { try { if (ResourceUtils.isFileURL(ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX))) { String path = ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath() + "urls.txt"; File dest = new File(path); FileUtils.writeLines(dest, StandardCharsets.UTF_8.name(), urls); Desktop.getDesktop().open(dest); } } catch (IOException e) { log.error(e.getMessage(), e); } } @Test public void test001() throws IOException { String jsonBody = webClient.get() .uri("https://00fly.online/upload/data.json") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON) .retrieve() .bodyToMono(String.class) .block() .replace("{", "{\n") .replace("}", "}\n") .replace(",", ",\n"); try (InputStream is = new ByteArrayInputStream(jsonBody.getBytes(StandardCharsets.UTF_8))) { List<String> urls = IOUtils.readLines(is, StandardCharsets.UTF_8).stream().filter(line -> StringUtils.contains(line, "\"url\":")).map(n -> StringUtils.substringBetween(n, ":\"", "\",")).collect(Collectors.toList()); log.info("★★★★★★★★ urls: {} ★★★★★★★★", urls.size()); process(urls); } } @Test public void test002() throws IOException { Resource resource = new ClassPathResource("data.json"); String jsonBody = IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8).replace("{", "{\n").replace("}", "}\n").replace(",", ",\n"); try (InputStream is = new ByteArrayInputStream(jsonBody.getBytes(StandardCharsets.UTF_8))) { List<String> urls = IOUtils.readLines(is, StandardCharsets.UTF_8).stream().filter(line -> StringUtils.contains(line, "\"url\":")).map(n -> StringUtils.substringBetween(n, ":\"", "\",")).collect(Collectors.toList()); log.info("★★★★★★★★ urls: {} ★★★★★★★★", urls.size()); process(urls); } } @Test public void test003() { String resp = webClient.get().uri("https://00fly.online/upload/urls.txt").acceptCharset(StandardCharsets.UTF_8).accept(MediaType.TEXT_HTML).retrieve().bodyToMono(String.class).block(); List<String> urls = Arrays.asList(StringUtils.split(resp, "\r\n")); AtomicInteger count = new AtomicInteger(0); urls.stream().forEach(url -> log.info("{}. {}", count.incrementAndGet(), url)); } } //goto src\test\java\com\fly\http\bean\ImageShowDialog.java package com.fly.http.bean; import java.awt.Dimension; import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JDialog; import javax.swing.JLabel; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; /** * * 弹出窗口 * * @author 00fly * @version [版本号, 2023年3月3日] * @see [相关类/方法] * @since [产品/模块版本] */ public class ImageShowDialog extends JDialog { private static final long serialVersionUID = -7240357454480002551L; public static void main(String[] args) throws IOException { Resource resources = new ClassPathResource("123.jpg"); BufferedImage image = ImageIO.read(resources.getInputStream()); new ImageShowDialog(image); } public ImageShowDialog(BufferedImage image) { super(); setTitle("图片查看工具"); setSize(image.getWidth(), image.getHeight() + 30); Dimension screenSize = getToolkit().getScreenSize(); Dimension dialogSize = getSize(); dialogSize.height = Math.min(screenSize.height, dialogSize.height); dialogSize.width = Math.min(screenSize.width, dialogSize.width); setLocation((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2); add(new JLabel(new ImageIcon(image))); setVisible(true); setResizable(false); setAlwaysOnTop(true); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } } //goto src\test\java\com\fly\http\bean\JsonBeanUtils.java package com.fly.http.bean; import java.io.IOException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; /** * JsonBean转换工具 * * @author 00fly * */ public class JsonBeanUtils { private static ObjectMapper objectMapper = new ObjectMapper(); /** * bean转json字符串 * * @param bean * @return * @throws IOException */ public static String beanToJson(Object bean) throws IOException { String jsonText = objectMapper.writeValueAsString(bean); return objectMapper.readTree(jsonText).toPrettyString(); } /** * bean转json字符串 * * @param bean * @param pretty 是否格式美化 * @return * @throws IOException */ public static String beanToJson(Object bean, boolean pretty) throws IOException { String jsonText = objectMapper.writeValueAsString(bean); if (pretty) { return objectMapper.readTree(jsonText).toPrettyString(); } return objectMapper.readTree(jsonText).toString(); } /** * json字符串转bean * * @param jsonText * @return * @throws IOException */ public static <T> T jsonToBean(String jsonText, Class<T> clazz) throws IOException { return objectMapper.readValue(jsonText, clazz); } /** * json字符串转bean * * @param jsonText * @return * @throws IOException */ public static <T> T jsonToBean(String jsonText, JavaType javaType) throws IOException { return objectMapper.readValue(jsonText, javaType); } /** * json字符串转bean * * @param jsonText * @return * @throws IOException */ public static <T> T jsonToBean(String jsonText, TypeReference<T> typeRef) throws IOException { return objectMapper.readValue(jsonText, typeRef); } } //goto src\test\java\com\fly\http\bean\SearchReq.java package com.fly.http.bean; import lombok.Data; @Data public class SearchReq { Integer pageNo = 1; Integer pageSize = 10; String keyword; } //goto src\test\java\com\fly\http\WebClientAsyncTest.java package com.fly.http; import java.awt.Desktop; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import org.apache.commons.lang3.RandomUtils; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.util.FileCopyUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.util.UriComponentsBuilder; import com.fly.http.bean.ImageShowDialog; import com.fly.http.bean.JsonBeanUtils; import com.fly.http.bean.SearchReq; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Mono; /** * http请求WebClient异步调用实现 */ @Slf4j public class WebClientAsyncTest { private WebClient webClient = WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build(); private void openImage(Resource resource) { try { new ImageShowDialog(ImageIO.read(resource.getInputStream())); } catch (IOException e) { log.error(e.getMessage(), e); } } @BeforeClass public static void init() { new File("download").mkdirs(); } /** * WebClient异步调用 * * @throws IOException * @throws InterruptedException */ @Test public void testDownFile() throws IOException, InterruptedException { Mono<Resource> mono = webClient.get() .uri("https://00fly.online/upload/urls.txt") .accept(MediaType.IMAGE_JPEG) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(Resource.class)); // 保存到本地 mono.subscribe(resource -> { try { FileCopyUtils.copy(resource.getInputStream(), new FileOutputStream("download/urls.txt")); } catch (IOException e) { log.error(e.getMessage(), e); } }); TimeUnit.SECONDS.sleep(2); } /** * WebClient异步调用 * * @throws IOException * @throws InterruptedException */ @Test public void testDownImg001() throws IOException, InterruptedException { Mono<Resource> mono = webClient.get() .uri("https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg") .accept(MediaType.IMAGE_JPEG) .retrieve() // 获取响应体 .bodyToMono(Resource.class); mono.subscribe(resource -> openImage(resource)); TimeUnit.SECONDS.sleep(10); } /** * WebClient异步调用 * * @throws IOException * @throws InterruptedException */ @Test public void testDownImg002() throws IOException, InterruptedException { Mono<Resource> mono = webClient.get() .uri("https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg") .accept(MediaType.IMAGE_JPEG) .retrieve() // 获取响应体 .bodyToMono(Resource.class); // 保存到本地 mono.subscribe(resource -> { try { File dest = new File(String.format("download/img_%s.jpg", System.currentTimeMillis())); FileCopyUtils.copy(resource.getInputStream(), new FileOutputStream(dest)); if (Desktop.isDesktopSupported()) { Desktop.getDesktop().open(dest.getParentFile()); } } catch (IOException e) { log.error(e.getMessage(), e); } }); TimeUnit.SECONDS.sleep(2); } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testExchange001() throws InterruptedException { // get MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<ClientResponse> monoGet = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https") .host("httpbin.org") .path("/get") .queryParams(params) // 等价 queryParam("q1", "java").queryParam("q2", "python") .build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange(); monoGet.subscribe(clientResponse -> { log.info("----- headers: {}", clientResponse.headers()); log.info("----- statusCode: {}", clientResponse.statusCode()); clientResponse.bodyToMono(String.class).subscribe(body -> log.info("----- reponse: {}", body)); }); TimeUnit.SECONDS.sleep(2); } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testExchange002() throws InterruptedException { // get Mono<ClientResponse> monoGet = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/get").queryParam("q1", "java").queryParam("q2", "python").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange(); monoGet.subscribe(clientResponse -> { log.info("----- headers: {}", clientResponse.headers()); log.info("----- statusCode: {}", clientResponse.statusCode()); clientResponse.bodyToMono(String.class).subscribe(body -> log.info("----- reponse: {}", body)); }); // formData post MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<ClientResponse> monoPost = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromFormData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .exchange(); monoPost.subscribe(clientResponse -> { log.info("----- headers: {}", clientResponse.headers()); log.info("----- statusCode: {}", clientResponse.statusCode()); clientResponse.bodyToMono(String.class).subscribe(body -> log.info("----- reponse: {}", body)); }); TimeUnit.SECONDS.sleep(2); } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testFormDataPost() throws InterruptedException { MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromFormData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testGet001() throws InterruptedException { Mono<String> mono = webClient.get() .uri("https://httpbin.org/{method}", "get") // {任意命名} .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testGet002() throws InterruptedException { Mono<ClientResponse> mono = webClient.get().uri("https://httpbin.org/get").acceptCharset(StandardCharsets.UTF_8).accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML).exchange(); mono.subscribe(reponse -> { log.info("----- headers: {}", reponse.headers()); log.info("----- statusCode: {}", reponse.statusCode()); reponse.bodyToMono(String.class).subscribe(body -> log.info("----- reponse: {}", body)); }); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testGet003() throws InterruptedException { Mono<String> mono = webClient.get() .uri("https://httpbin.org/get") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用, https://httpbin.org/get?q=java * * @throws InterruptedException */ @Test public void testGet004() throws InterruptedException { MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q", "java"); String uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParams(params).toUriString(); // 注意比较 // uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParam("q", "java", "python").toUriString(); // uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParam("q1", "java").queryParam("q2", "python").toUriString(); Mono<String> mono = webClient.get() .uri(uri) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testGet005() throws InterruptedException { Mono<String> mono = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/get").queryParam("q1", "java").queryParam("q2", "python").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testJsonBody001() throws InterruptedException { Mono<String> mono = webClient.post() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/post").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .contentType(MediaType.APPLICATION_JSON) .bodyValue(Collections.singletonMap("q", "java")) .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws IOException * @throws InterruptedException */ @Test public void testJsonBody002() throws IOException, InterruptedException { Mono<String> mono; int num = RandomUtils.nextInt(1, 4); switch (num) { case 1: // 方式1,javaBean SearchReq req = new SearchReq(); req.setPageNo(1); req.setPageSize(10); req.setKeyword("1"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .bodyValue(req) // 设置JsonBody .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("reponse: {}", body)); break; case 2: // 方式2,HashMap Map<String, String> params = new HashMap<>(); params.put("pageNo", "2"); params.put("pageSize", "20"); params.put("keyword", "2"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .bodyValue(params) // 设置JsonBody .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("reponse: {}", body)); break; case 3: // 方式3,json字符串 Map<String, String> params2 = new HashMap<>(); params2.put("pageNo", "2"); params2.put("pageSize", "20"); params2.put("keyword", "2"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromValue(JsonBeanUtils.beanToJson(params2, false))) // 设置formData .retrieve() .bodyToMono(String.class); mono.subscribe(body -> log.info("reponse: {}", body)); break; } TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testUpload001() throws InterruptedException { MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); params.add("file", new ClassPathResource("123.jpg")); Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromMultipartData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } /** * WebClient异步调用 * * @throws InterruptedException */ @Test public void testUpload002() throws InterruptedException { Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromMultipartData("q1", "java").with("q2", "python").with("file", new ClassPathResource("123.jpg"))) .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 mono.subscribe(body -> log.info("----- reponse: {}", body)); TimeUnit.SECONDS.sleep(2); // 重要,等待异步调用完成 } } //goto src\test\java\com\fly\http\WebClientSyncTest.java package com.fly.http; import java.awt.Desktop; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import org.apache.commons.lang3.RandomUtils; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.util.FileCopyUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.util.UriComponentsBuilder; import com.fly.http.bean.ImageShowDialog; import com.fly.http.bean.JsonBeanUtils; import com.fly.http.bean.SearchReq; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Mono; /** * http请求WebClient同步调用实现 */ @Slf4j public class WebClientSyncTest { private WebClient webClient = WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build(); private void openImage(Resource resource) { try { new ImageShowDialog(ImageIO.read(resource.getInputStream())); } catch (IOException e) { log.error(e.getMessage(), e); } } @BeforeClass public static void init() { new File("download").mkdirs(); } /** * WebClient同步调用 * * @throws IOException */ @Test public void testDownFile() throws IOException { Mono<ClientResponse> mono = webClient.get().uri("https://00fly.online/upload/urls.txt").accept(MediaType.IMAGE_JPEG).exchange(); ClientResponse response = mono.block(); log.info("----- headers: {}", response.headers()); log.info("----- statusCode: {}", response.statusCode()); // 保存到本地 Resource resource = response.bodyToMono(Resource.class).block(); FileCopyUtils.copy(resource.getInputStream(), new FileOutputStream("download/urls.txt")); } /** * WebClient同步调用 * * @throws IOException * @throws InterruptedException */ @Test public void testDownImg001() throws IOException, InterruptedException { Mono<Resource> mono = webClient.get() .uri("https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg") .accept(MediaType.IMAGE_JPEG) .retrieve() // 获取响应体 .bodyToMono(Resource.class); openImage(mono.block()); TimeUnit.SECONDS.sleep(10); } /** * WebClient同步调用 * * @throws IOException */ @Test public void testDownImg002() throws IOException { Mono<Resource> mono = webClient.get() .uri("https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg") .accept(MediaType.IMAGE_JPEG) .retrieve() // 获取响应体 .bodyToMono(Resource.class); // 保存到本地 Resource resource = mono.block(); File dest = new File(String.format("download/img_%s.jpg", System.currentTimeMillis())); FileCopyUtils.copy(resource.getInputStream(), new FileOutputStream(dest)); if (Desktop.isDesktopSupported()) { Desktop.getDesktop().open(dest.getParentFile()); } } /** * WebClient同步调用 */ @Test public void testExchange001() { // get MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<ClientResponse> monoGet = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https") .host("httpbin.org") .path("/get") .queryParams(params) // 等价 queryParam("q1", "java").queryParam("q2", "python") .build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange(); ClientResponse clientResponse = monoGet.block(); // 获取完整的响应对象 log.info("----- headers: {}", clientResponse.headers()); log.info("----- statusCode: {}", clientResponse.statusCode()); log.info("----- reponse: {}", clientResponse.bodyToMono(String.class).block()); } /** * WebClient同步调用 */ @Test public void testExchange002() { // get Mono<ClientResponse> monoGet = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/get").queryParam("q1", "java").queryParam("q2", "python").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange(); ClientResponse clientResponse = monoGet.block(); // 获取完整的响应对象 log.info("----- headers: {}", clientResponse.headers()); log.info("----- statusCode: {}", clientResponse.statusCode()); log.info("----- reponse: {}", clientResponse.bodyToMono(String.class).block()); // formData post MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<ClientResponse> monoPost = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromFormData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .exchange(); ClientResponse clientResponse2 = monoPost.block(); // 获取完整的响应对象 log.info("----- headers: {}", clientResponse2.headers()); log.info("----- statusCode: {}", clientResponse2.statusCode()); log.info("----- reponse: {}", clientResponse2.bodyToMono(String.class).block()); } /** * WebClient同步调用 */ @Test public void testFormDataPost() { MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromFormData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 */ @Test public void testGet001() { Mono<String> mono = webClient.get() .uri("https://httpbin.org/{method}", "get") // {任意命名} .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 */ @Test public void testGet002() { Mono<String> mono = webClient.get() .uri("https://httpbin.org/get") .acceptCharset(StandardCharsets.UTF_8) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 * */ @Test public void testGet003() { Mono<String> mono = webClient.get() .uri("https://httpbin.org/get") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用, https://httpbin.org/get?q=java * */ @Test public void testGet004() { MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("q", "java"); String uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParams(params).toUriString(); // 注意比较 // uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParam("q", "java", "python").toUriString(); // uri = UriComponentsBuilder.fromUriString("https://httpbin.org/get").queryParam("q1", "java").queryParam("q2", "python").toUriString(); Mono<String> mono = webClient.get() .uri(uri) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .exchange() .doOnSuccess(clientResponse -> log.info("----- headers: {}", clientResponse.headers())) .doOnSuccess(clientResponse -> log.info("----- statusCode: {}", clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 * */ @Test public void testGet005() { Mono<String> mono = webClient.get() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/get").queryParam("q1", "java").queryParam("q2", "python").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 * */ @Test public void testJsonBody001() { Mono<String> mono = webClient.post() .uri(uriBuilder -> uriBuilder.scheme("https").host("httpbin.org").path("/post").build()) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .contentType(MediaType.APPLICATION_JSON) .bodyValue(Collections.singletonMap("q", "java")) .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); } /** * WebClient同步调用 * * @throws IOException */ @Test public void testJsonBody002() throws IOException { Mono<String> mono; int num = RandomUtils.nextInt(1, 4); switch (num) { case 1: // 方式1,javaBean SearchReq req = new SearchReq(); req.setPageNo(1); req.setPageSize(10); req.setKeyword("1"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .bodyValue(req) // 设置JsonBody .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); break; case 2: // 方式2,HashMap Map<String, String> params = new HashMap<>(); params.put("pageNo", "2"); params.put("pageSize", "20"); params.put("keyword", "2"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .bodyValue(params) // 设置JsonBody .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); break; case 3: // 方式3,json字符串 Map<String, String> params2 = new HashMap<>(); params2.put("pageNo", "2"); params2.put("pageSize", "20"); params2.put("keyword", "2"); mono = webClient.post() .uri("https://httpbin.org/post") .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromValue(JsonBeanUtils.beanToJson(params2, false))) // 设置formData .retrieve() .bodyToMono(String.class); log.info("reponse: {}", mono.block()); break; } } /** * WebClient同步调用 * */ @Test public void testUpload001() { MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); params.add("q1", "java"); params.add("q2", "python"); params.add("file", new ClassPathResource("123.jpg")); Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromMultipartData(params)) // 设置formData,等价 BodyInserters.fromFormData("q1", "java").with("q2", "python") .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 log.info("----- reponse: {}", mono.block()); } /** * WebClient同步调用 * */ @Test public void testUpload002() { Mono<String> mono = webClient.post() .uri("https://httpbin.org/post") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .acceptCharset(StandardCharsets.UTF_8) .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML) .body(BodyInserters.fromMultipartData("q1", "java").with("q2", "python").with("file", new ClassPathResource("123.jpg"))) .retrieve() // 获取响应体 .bodyToMono(String.class); // 响应数据类型转换 log.info("----- reponse: {}", mono.block()); } }
  • 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
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
  • 706
  • 707
  • 708
  • 709
  • 710
  • 711
  • 712
  • 713
  • 714
  • 715
  • 716
  • 717
  • 718
  • 719
  • 720
  • 721
  • 722
  • 723
  • 724
  • 725
  • 726
  • 727
  • 728
  • 729
  • 730
  • 731
  • 732
  • 733
  • 734
  • 735
  • 736
  • 737
  • 738
  • 739
  • 740
  • 741
  • 742
  • 743
  • 744
  • 745
  • 746
  • 747
  • 748
  • 749
  • 750
  • 751
  • 752
  • 753
  • 754
  • 755
  • 756
  • 757
  • 758
  • 759
  • 760
  • 761
  • 762
  • 763
  • 764
  • 765
  • 766
  • 767
  • 768
  • 769
  • 770
  • 771
  • 772
  • 773
  • 774
  • 775
  • 776
  • 777
  • 778
  • 779
  • 780
  • 781
  • 782
  • 783
  • 784
  • 785
  • 786
  • 787
  • 788
  • 789
  • 790
  • 791
  • 792
  • 793
  • 794
  • 795
  • 796
  • 797
  • 798
  • 799
  • 800
  • 801
  • 802
  • 803
  • 804
  • 805
  • 806
  • 807
  • 808
  • 809
  • 810
  • 811
  • 812
  • 813
  • 814
  • 815
  • 816
  • 817
  • 818
  • 819
  • 820
  • 821
  • 822
  • 823
  • 824
  • 825
  • 826
  • 827
  • 828
  • 829
  • 830
  • 831
  • 832
  • 833
  • 834
  • 835
  • 836
  • 837
  • 838
  • 839
  • 840
  • 841
  • 842
  • 843
  • 844
  • 845
  • 846
  • 847
  • 848
  • 849
  • 850
  • 851
  • 852
  • 853
  • 854
  • 855
  • 856
  • 857
  • 858
  • 859
  • 860
  • 861
  • 862
  • 863
  • 864
  • 865
  • 866
  • 867
  • 868
  • 869
  • 870
  • 871
  • 872
  • 873
  • 874
  • 875
  • 876
  • 877
  • 878
  • 879
  • 880
  • 881
  • 882
  • 883
  • 884
  • 885
  • 886
  • 887
  • 888
  • 889
  • 890
  • 891
  • 892
  • 893
  • 894
  • 895
  • 896
  • 897
  • 898
  • 899
  • 900
  • 901
  • 902
  • 903
  • 904
  • 905
  • 906
  • 907
  • 908
  • 909
  • 910
  • 911
  • 912
  • 913
  • 914
  • 915
  • 916
  • 917
  • 918
  • 919
  • 920
  • 921
  • 922
  • 923
  • 924
  • 925
  • 926
  • 927
  • 928
  • 929
  • 930
  • 931
  • 932
  • 933
  • 934
  • 935
  • 936
  • 937
  • 938
  • 939
  • 940
  • 941
  • 942
  • 943
  • 944
  • 945
  • 946
  • 947
  • 948
  • 949
  • 950
  • 951
  • 952
  • 953
  • 954
  • 955
  • 956
  • 957
  • 958
  • 959
  • 960
  • 961
  • 962
  • 963
  • 964
  • 965
  • 966
  • 967
  • 968
  • 969
  • 970
  • 971
  • 972
  • 973
  • 974
  • 975
  • 976
  • 977
  • 978
  • 979
  • 980
  • 981
  • 982
  • 983
  • 984
  • 985
  • 986
  • 987
  • 988
  • 989
  • 990
  • 991
  • 992
  • 993
  • 994
  • 995
  • 996
  • 997
  • 998
  • 999
  • 1000
  • 1001
  • 1002
  • 1003
  • 1004
  • 1005
  • 1006
  • 1007
  • 1008
  • 1009
  • 1010
  • 1011
  • 1012
  • 1013
  • 1014
  • 1015
  • 1016
  • 1017
  • 1018
  • 1019
  • 1020
  • 1021
  • 1022
  • 1023
  • 1024
  • 1025
  • 1026
  • 1027
  • 1028
  • 1029
  • 1030
  • 1031
  • 1032
  • 1033
  • 1034
  • 1035
  • 1036
  • 1037
  • 1038
  • 1039
  • 1040
  • 1041
  • 1042
  • 1043
  • 1044
  • 1045
  • 1046
  • 1047
  • 1048
  • 1049
  • 1050
  • 1051
  • 1052
  • 1053
  • 1054
  • 1055
  • 1056
  • 1057
  • 1058
  • 1059
  • 1060
  • 1061
  • 1062
  • 1063
  • 1064
  • 1065
  • 1066
  • 1067
  • 1068
  • 1069
  • 1070
  • 1071
  • 1072
  • 1073
  • 1074
  • 1075
  • 1076
  • 1077
  • 1078
  • 1079
  • 1080
  • 1081
  • 1082
  • 1083
  • 1084
  • 1085
  • 1086
  • 1087
  • 1088
  • 1089
  • 1090
  • 1091
  • 1092
  • 1093
  • 1094
  • 1095
  • 1096
  • 1097
  • 1098
  • 1099
  • 1100
  • 1101
  • 1102
  • 1103
  • 1104
  • 1105
  • 1106
  • 1107
  • 1108
  • 1109
  • 1110
  • 1111
  • 1112
  • 1113
  • 1114
  • 1115
  • 1116
  • 1117
  • 1118
  • 1119
  • 1120
  • 1121
  • 1122
  • 1123
  • 1124
  • 1125
  • 1126
  • 1127
  • 1128
  • 1129
  • 1130
  • 1131
  • 1132
  • 1133
  • 1134
  • 1135
  • 1136
  • 1137
  • 1138
  • 1139
  • 1140
  • 1141
  • 1142
  • 1143
  • 1144
  • 1145
  • 1146
  • 1147
  • 1148
  • 1149
  • 1150
  • 1151
  • 1152
  • 1153
  • 1154
  • 1155
  • 1156
  • 1157
  • 1158
  • 1159
  • 1160
  • 1161
  • 1162
  • 1163
  • 1164
  • 1165
  • 1166
  • 1167
  • 1168
  • 1169
  • 1170
  • 1171
  • 1172
  • 1173
  • 1174
  • 1175
  • 1176
  • 1177
  • 1178
  • 1179
  • 1180
  • 1181
  • 1182
  • 1183
  • 1184
  • 1185
  • 1186
  • 1187
  • 1188
  • 1189
  • 1190
  • 1191
  • 1192
  • 1193
  • 1194
  • 1195
  • 1196
  • 1197
  • 1198
  • 1199
  • 1200
  • 1201
  • 1202
  • 1203
  • 1204
  • 1205
  • 1206
  • 1207
  • 1208
  • 1209
  • 1210
  • 1211
  • 1212
  • 1213
  • 1214
  • 1215
  • 1216
  • 1217
  • 1218
  • 1219
  • 1220
  • 1221
  • 1222
  • 1223
  • 1224
  • 1225
  • 1226
  • 1227
  • 1228
  • 1229
  • 1230
  • 1231
  • 1232
  • 1233
  • 1234
  • 1235
  • 1236
  • 1237
  • 1238
  • 1239
  • 1240
  • 1241
  • 1242
  • 1243
  • 1244
  • 1245
  • 1246
  • 1247
  • 1248
  • 1249
  • 1250
  • 1251
  • 1252
  • 1253
  • 1254
  • 1255
  • 1256
  • 1257
  • 1258
  • 1259
  • 1260
  • 1261
  • 1262
  • 1263
  • 1264
  • 1265
  • 1266
  • 1267
  • 1268
  • 1269
  • 1270
  • 1271
  • 1272
  • 1273
  • 1274
  • 1275
  • 1276
  • 1277
  • 1278
  • 1279
  • 1280
  • 1281
  • 1282
  • 1283
  • 1284
  • 1285
  • 1286
  • 1287
  • 1288
  • 1289
  • 1290
  • 1291
  • 1292
  • 1293
  • 1294
  • 1295
  • 1296
  • 1297
  • 1298
  • 1299
  • 1300
  • 1301
  • 1302
  • 1303
  • 1304
  • 1305
  • 1306
  • 1307
  • 1308
  • 1309
  • 1310
  • 1311
  • 1312
  • 1313
  • 1314
  • 1315
  • 1316
  • 1317
  • 1318
  • 1319
  • 1320
  • 1321
  • 1322
  • 1323
  • 1324
  • 1325
  • 1326
  • 1327
  • 1328
  • 1329
  • 1330
  • 1331
  • 1332
  • 1333
  • 1334
  • 1335
  • 1336
  • 1337
  • 1338
  • 1339
  • 1340
  • 1341
  • 1342
  • 1343
  • 1344
  • 1345
  • 1346
  • 1347
  • 1348
  • 1349
  • 1350
  • 1351
  • 1352
  • 1353
  • 1354
  • 1355
  • 1356
  • 1357
  • 1358
  • 1359
  • 1360
  • 1361
  • 1362
  • 1363
  • 1364
  • 1365
  • 1366
  • 1367
  • 1368
  • 1369
  • 1370
  • 1371
  • 1372
  • 1373
  • 1374
  • 1375
  • 1376
  • 1377
  • 1378
  • 1379
  • 1380
  • 1381
  • 1382
  • 1383
  • 1384
  • 1385
  • 1386
  • 1387
  • 1388
  • 1389
  • 1390
  • 1391
  • 1392
  • 1393
  • 1394
  • 1395
  • 1396
  • 1397
  • 1398
  • 1399
  • 1400
  • 1401
  • 1402
  • 1403
  • 1404
  • 1405
  • 1406
  • 1407
  • 1408
  • 1409
  • 1410
  • 1411
  • 1412
  • 1413
  • 1414
  • 1415
  • 1416
  • 1417
  • 1418
  • 1419
  • 1420
  • 1421
  • 1422
  • 1423
  • 1424
  • 1425
  • 1426
  • 1427
  • 1428
  • 1429
  • 1430
  • 1431
  • 1432
  • 1433
  • 1434
  • 1435
  • 1436
  • 1437
  • 1438
  • 1439
  • 1440
  • 1441
  • 1442
  • 1443
  • 1444
  • 1445
  • 1446

有任何问题和建议,都可以向我提问讨论,大家一起进步,谢谢!

-over-

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索