0


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

文章目录

一、概述

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

二、pom依赖

<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>

三、代码结构

在这里插入图片描述

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

四、源码传送

1、异步代码

importjava.awt.Desktop;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.nio.charset.StandardCharsets;importjava.util.Collections;importjava.util.HashMap;importjava.util.Map;importjava.util.concurrent.TimeUnit;importjavax.imageio.ImageIO;importorg.apache.commons.lang3.RandomUtils;importorg.junit.BeforeClass;importorg.junit.Test;importorg.springframework.core.io.ClassPathResource;importorg.springframework.core.io.Resource;importorg.springframework.http.MediaType;importorg.springframework.util.FileCopyUtils;importorg.springframework.util.LinkedMultiValueMap;importorg.springframework.util.MultiValueMap;importorg.springframework.web.reactive.function.BodyInserters;importorg.springframework.web.reactive.function.client.ClientResponse;importorg.springframework.web.reactive.function.client.WebClient;importorg.springframework.web.util.UriComponentsBuilder;importcom.fly.http.bean.ImageShowDialog;importcom.fly.http.bean.JsonBeanUtils;importcom.fly.http.bean.SearchReq;importlombok.extern.slf4j.Slf4j;importreactor.core.publisher.Mono;/**
 * http请求WebClient异步调用实现
 */@Slf4jpublicclassWebClientAsyncTest{privateWebClient webClient =WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build();privatevoidopenImage(Resource resource){try{newImageShowDialog(ImageIO.read(resource.getInputStream()));}catch(IOException e){
            log.error(e.getMessage(), e);}}@BeforeClasspublicstaticvoidinit(){newFile("download").mkdirs();}/**
     * WebClient异步调用
     * 
     * @throws IOException
     * @throws InterruptedException
     */@TestpublicvoidtestDownFile()throwsIOException,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(),newFileOutputStream("download/urls.txt"));}catch(IOException e){
                log.error(e.getMessage(), e);}});TimeUnit.SECONDS.sleep(2);}/**
     * WebClient异步调用
     * 
     * @throws IOException
     * @throws InterruptedException
     */@TestpublicvoidtestDownImg001()throwsIOException,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
     */@TestpublicvoidtestDownImg002()throwsIOException,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 =newFile(String.format("download/img_%s.jpg",System.currentTimeMillis()));FileCopyUtils.copy(resource.getInputStream(),newFileOutputStream(dest));if(Desktop.isDesktopSupported()){Desktop.getDesktop().open(dest.getParentFile());}}catch(IOException e){
                log.error(e.getMessage(), e);}});TimeUnit.SECONDS.sleep(2);}/**
     * WebClient异步调用
     * 
     * @throws InterruptedException
     */@TestpublicvoidtestExchange001()throwsInterruptedException{// getMultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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
     */@TestpublicvoidtestExchange002()throwsInterruptedException{// getMono<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 postMultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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
     */@TestpublicvoidtestFormDataPost()throwsInterruptedException{MultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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
     */@TestpublicvoidtestGet001()throwsInterruptedException{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
     */@TestpublicvoidtestGet002()throwsInterruptedException{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
     */@TestpublicvoidtestGet003()throwsInterruptedException{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
     */@TestpublicvoidtestGet004()throwsInterruptedException{MultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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
     */@TestpublicvoidtestGet005()throwsInterruptedException{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
     */@TestpublicvoidtestJsonBody001()throwsInterruptedException{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
     */@TestpublicvoidtestJsonBody002()throwsIOException,InterruptedException{Mono<String> mono;int num =RandomUtils.nextInt(1,4);switch(num){case1:// 方式1,javaBeanSearchReq req =newSearchReq();
                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;case2:// 方式2,HashMapMap<String,String> params =newHashMap<>();
                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;case3:// 方式3,json字符串Map<String,String> params2 =newHashMap<>();
                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
     */@TestpublicvoidtestUpload001()throwsInterruptedException{MultiValueMap<String,Object> params =newLinkedMultiValueMap<>();
        params.add("q1","java");
        params.add("q2","python");
        params.add("file",newClassPathResource("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
     */@TestpublicvoidtestUpload002()throwsInterruptedException{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",newClassPathResource("123.jpg"))).retrieve()// 获取响应体.bodyToMono(String.class);// 响应数据类型转换
        mono.subscribe(body -> log.info("----- reponse: {}", body));TimeUnit.SECONDS.sleep(2);// 重要,等待异步调用完成}}

2、同步代码

importjava.awt.Desktop;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.nio.charset.StandardCharsets;importjava.util.Collections;importjava.util.HashMap;importjava.util.Map;importjava.util.concurrent.TimeUnit;importjavax.imageio.ImageIO;importorg.apache.commons.lang3.RandomUtils;importorg.junit.BeforeClass;importorg.junit.Test;importorg.springframework.core.io.ClassPathResource;importorg.springframework.core.io.Resource;importorg.springframework.http.MediaType;importorg.springframework.util.FileCopyUtils;importorg.springframework.util.LinkedMultiValueMap;importorg.springframework.util.MultiValueMap;importorg.springframework.web.reactive.function.BodyInserters;importorg.springframework.web.reactive.function.client.ClientResponse;importorg.springframework.web.reactive.function.client.WebClient;importorg.springframework.web.util.UriComponentsBuilder;importcom.fly.http.bean.ImageShowDialog;importcom.fly.http.bean.JsonBeanUtils;importcom.fly.http.bean.SearchReq;importlombok.extern.slf4j.Slf4j;importreactor.core.publisher.Mono;/**
 * http请求WebClient同步调用实现
 */@Slf4jpublicclassWebClientSyncTest{privateWebClient webClient =WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build();privatevoidopenImage(Resource resource){try{newImageShowDialog(ImageIO.read(resource.getInputStream()));}catch(IOException e){
            log.error(e.getMessage(), e);}}@BeforeClasspublicstaticvoidinit(){newFile("download").mkdirs();}/**
     * WebClient同步调用
     * 
     * @throws IOException
     */@TestpublicvoidtestDownFile()throwsIOException{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(),newFileOutputStream("download/urls.txt"));}/**
     * WebClient同步调用
     * 
     * @throws IOException
     * @throws InterruptedException
     */@TestpublicvoidtestDownImg001()throwsIOException,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
     */@TestpublicvoidtestDownImg002()throwsIOException{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 =newFile(String.format("download/img_%s.jpg",System.currentTimeMillis()));FileCopyUtils.copy(resource.getInputStream(),newFileOutputStream(dest));if(Desktop.isDesktopSupported()){Desktop.getDesktop().open(dest.getParentFile());}}/**
     * WebClient同步调用
     */@TestpublicvoidtestExchange001(){// getMultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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同步调用
     */@TestpublicvoidtestExchange002(){// getMono<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 postMultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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同步调用
     */@TestpublicvoidtestFormDataPost(){MultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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同步调用
     */@TestpublicvoidtestGet001(){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同步调用
     */@TestpublicvoidtestGet002(){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同步调用
     * 
     */@TestpublicvoidtestGet003(){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
     * 
     */@TestpublicvoidtestGet004(){MultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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同步调用
     * 
     */@TestpublicvoidtestGet005(){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同步调用
     * 
     */@TestpublicvoidtestJsonBody001(){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
     */@TestpublicvoidtestJsonBody002()throwsIOException{Mono<String> mono;int num =RandomUtils.nextInt(1,4);switch(num){case1:// 方式1,javaBeanSearchReq req =newSearchReq();
                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;case2:// 方式2,HashMapMap<String,String> params =newHashMap<>();
                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;case3:// 方式3,json字符串Map<String,String> params2 =newHashMap<>();
                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同步调用
     * 
     */@TestpublicvoidtestUpload001(){MultiValueMap<String,Object> params =newLinkedMultiValueMap<>();
        params.add("q1","java");
        params.add("q2","python");
        params.add("file",newClassPathResource("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同步调用
     * 
     */@TestpublicvoidtestUpload002(){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",newClassPathResource("123.jpg"))).retrieve()// 获取响应体.bodyToMono(String.class);// 响应数据类型转换
        log.info("----- reponse: {}", mono.block());}}

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 DockerfileFROM 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.javapackagecom.fly.http;importjava.nio.charset.StandardCharsets;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.List;importjava.util.concurrent.atomic.AtomicInteger;importorg.apache.commons.lang3.RandomUtils;importorg.apache.commons.lang3.StringUtils;importorg.springframework.http.MediaType;importorg.springframework.web.reactive.function.client.WebClient;importlombok.extern.slf4j.Slf4j;/**
 * WebClient是RestTemplete的替代品,有更好的响应式能力,支持异步调用<br>
 * 
 * https://blog.csdn.net/zzhongcy/article/details/105412842
 * 
 */@Slf4jpublicclassFluxWebClient{privateList<String> urls =newArrayList<>();// 缓冲区默认256k,设为-1以解决报错Exceeded limit on max bytes to buffer : 262144privateWebClient webClient =WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build();publicvoidvisitAll(){// 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 =newAtomicInteger(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.javapackagecom.fly.http;importjava.util.Timer;importjava.util.TimerTask;importjava.util.concurrent.ScheduledThreadPoolExecutor;importjava.util.concurrent.TimeUnit;importlombok.extern.slf4j.Slf4j;@Slf4jpublicclassRunMain{privatestaticFluxWebClient webClient =newFluxWebClient();/**
     * 程序运行入口
     * 
     */publicstaticvoidmain(String[] args){scheduledThreadPoolExecutorStart();}privatestaticvoidscheduledThreadPoolExecutorStart(){newScheduledThreadPoolExecutor(2).scheduleAtFixedRate(()->{
            webClient.visitAll();},0L,30,TimeUnit.SECONDS);
        log.info("======== ScheduledThreadPoolExecutor started!");}/**
     * Timer线程安全, 但单线程执行, 抛出异常时, task会终止
     */@DeprecatedprotectedstaticvoidtimeStart(){newTimer().scheduleAtFixedRate(newTimerTask(){@Overridepublicvoidrun(){
                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.javapackagecom.fly.http;importjava.awt.Desktop;importjava.io.ByteArrayInputStream;importjava.io.File;importjava.io.IOException;importjava.io.InputStream;importjava.nio.charset.StandardCharsets;importjava.util.Arrays;importjava.util.List;importjava.util.concurrent.atomic.AtomicInteger;importjava.util.stream.Collectors;importorg.apache.commons.io.FileUtils;importorg.apache.commons.io.IOUtils;importorg.apache.commons.lang3.StringUtils;importorg.junit.Test;importorg.springframework.core.io.ClassPathResource;importorg.springframework.core.io.Resource;importorg.springframework.http.MediaType;importorg.springframework.util.ResourceUtils;importorg.springframework.web.reactive.function.client.WebClient;importlombok.extern.slf4j.Slf4j;@Slf4jpublicclassApiTest{// 缓冲区默认256k,设为-1以解决报错Exceeded limit on max bytes to buffer : 262144privateWebClient webClient =WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build();/**
     * 写入文本文件
     * 
     * @param urls
     * @see [类、类#方法、类#成员]
     */privatevoidprocess(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 =newFile(path);FileUtils.writeLines(dest,StandardCharsets.UTF_8.name(), urls);Desktop.getDesktop().open(dest);}}catch(IOException e){
            log.error(e.getMessage(), e);}}@Testpublicvoidtest001()throwsIOException{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 =newByteArrayInputStream(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);}}@Testpublicvoidtest002()throwsIOException{Resource resource =newClassPathResource("data.json");String jsonBody =IOUtils.toString(resource.getInputStream(),StandardCharsets.UTF_8).replace("{","{\n").replace("}","}\n").replace(",",",\n");try(InputStream is =newByteArrayInputStream(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);}}@Testpublicvoidtest003(){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 =newAtomicInteger(0);
        urls.stream().forEach(url -> log.info("{}. {}", count.incrementAndGet(), url));}}//goto src\test\java\com\fly\http\bean\ImageShowDialog.javapackagecom.fly.http.bean;importjava.awt.Dimension;importjava.awt.image.BufferedImage;importjava.io.IOException;importjavax.imageio.ImageIO;importjavax.swing.ImageIcon;importjavax.swing.JDialog;importjavax.swing.JLabel;importorg.springframework.core.io.ClassPathResource;importorg.springframework.core.io.Resource;/**
 * 
 * 弹出窗口
 * 
 * @author 00fly
 * @version [版本号, 2023年3月3日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */publicclassImageShowDialogextendsJDialog{privatestaticfinallong serialVersionUID =-7240357454480002551L;publicstaticvoidmain(String[] args)throwsIOException{Resource resources =newClassPathResource("123.jpg");BufferedImage image =ImageIO.read(resources.getInputStream());newImageShowDialog(image);}publicImageShowDialog(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(newJLabel(newImageIcon(image)));setVisible(true);setResizable(false);setAlwaysOnTop(true);setDefaultCloseOperation(DISPOSE_ON_CLOSE);}}//goto src\test\java\com\fly\http\bean\JsonBeanUtils.javapackagecom.fly.http.bean;importjava.io.IOException;importcom.fasterxml.jackson.core.type.TypeReference;importcom.fasterxml.jackson.databind.JavaType;importcom.fasterxml.jackson.databind.ObjectMapper;/**
 * JsonBean转换工具
 * 
 * @author 00fly
 *
 */publicclassJsonBeanUtils{privatestaticObjectMapper objectMapper =newObjectMapper();/**
     * bean转json字符串
     * 
     * @param bean
     * @return
     * @throws IOException
     */publicstaticStringbeanToJson(Object bean)throwsIOException{String jsonText = objectMapper.writeValueAsString(bean);return objectMapper.readTree(jsonText).toPrettyString();}/**
     * bean转json字符串
     * 
     * @param bean
     * @param pretty 是否格式美化
     * @return
     * @throws IOException
     */publicstaticStringbeanToJson(Object bean,boolean pretty)throwsIOException{String jsonText = objectMapper.writeValueAsString(bean);if(pretty){return objectMapper.readTree(jsonText).toPrettyString();}return objectMapper.readTree(jsonText).toString();}/**
     * json字符串转bean
     * 
     * @param jsonText
     * @return
     * @throws IOException
     */publicstatic<T>TjsonToBean(String jsonText,Class<T> clazz)throwsIOException{return objectMapper.readValue(jsonText, clazz);}/**
     * json字符串转bean
     * 
     * @param jsonText
     * @return
     * @throws IOException
     */publicstatic<T>TjsonToBean(String jsonText,JavaType javaType)throwsIOException{return objectMapper.readValue(jsonText, javaType);}/**
     * json字符串转bean
     * 
     * @param jsonText
     * @return
     * @throws IOException
     */publicstatic<T>TjsonToBean(String jsonText,TypeReference<T> typeRef)throwsIOException{return objectMapper.readValue(jsonText, typeRef);}}//goto src\test\java\com\fly\http\bean\SearchReq.javapackagecom.fly.http.bean;importlombok.Data;@DatapublicclassSearchReq{Integer pageNo =1;Integer pageSize =10;String keyword;}//goto src\test\java\com\fly\http\WebClientAsyncTest.javapackagecom.fly.http;importjava.awt.Desktop;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.nio.charset.StandardCharsets;importjava.util.Collections;importjava.util.HashMap;importjava.util.Map;importjava.util.concurrent.TimeUnit;importjavax.imageio.ImageIO;importorg.apache.commons.lang3.RandomUtils;importorg.junit.BeforeClass;importorg.junit.Test;importorg.springframework.core.io.ClassPathResource;importorg.springframework.core.io.Resource;importorg.springframework.http.MediaType;importorg.springframework.util.FileCopyUtils;importorg.springframework.util.LinkedMultiValueMap;importorg.springframework.util.MultiValueMap;importorg.springframework.web.reactive.function.BodyInserters;importorg.springframework.web.reactive.function.client.ClientResponse;importorg.springframework.web.reactive.function.client.WebClient;importorg.springframework.web.util.UriComponentsBuilder;importcom.fly.http.bean.ImageShowDialog;importcom.fly.http.bean.JsonBeanUtils;importcom.fly.http.bean.SearchReq;importlombok.extern.slf4j.Slf4j;importreactor.core.publisher.Mono;/**
 * http请求WebClient异步调用实现
 */@Slf4jpublicclassWebClientAsyncTest{privateWebClient webClient =WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build();privatevoidopenImage(Resource resource){try{newImageShowDialog(ImageIO.read(resource.getInputStream()));}catch(IOException e){
            log.error(e.getMessage(), e);}}@BeforeClasspublicstaticvoidinit(){newFile("download").mkdirs();}/**
     * WebClient异步调用
     * 
     * @throws IOException
     * @throws InterruptedException
     */@TestpublicvoidtestDownFile()throwsIOException,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(),newFileOutputStream("download/urls.txt"));}catch(IOException e){
                log.error(e.getMessage(), e);}});TimeUnit.SECONDS.sleep(2);}/**
     * WebClient异步调用
     * 
     * @throws IOException
     * @throws InterruptedException
     */@TestpublicvoidtestDownImg001()throwsIOException,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
     */@TestpublicvoidtestDownImg002()throwsIOException,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 =newFile(String.format("download/img_%s.jpg",System.currentTimeMillis()));FileCopyUtils.copy(resource.getInputStream(),newFileOutputStream(dest));if(Desktop.isDesktopSupported()){Desktop.getDesktop().open(dest.getParentFile());}}catch(IOException e){
                log.error(e.getMessage(), e);}});TimeUnit.SECONDS.sleep(2);}/**
     * WebClient异步调用
     * 
     * @throws InterruptedException
     */@TestpublicvoidtestExchange001()throwsInterruptedException{// getMultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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
     */@TestpublicvoidtestExchange002()throwsInterruptedException{// getMono<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 postMultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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
     */@TestpublicvoidtestFormDataPost()throwsInterruptedException{MultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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
     */@TestpublicvoidtestGet001()throwsInterruptedException{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
     */@TestpublicvoidtestGet002()throwsInterruptedException{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
     */@TestpublicvoidtestGet003()throwsInterruptedException{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
     */@TestpublicvoidtestGet004()throwsInterruptedException{MultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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
     */@TestpublicvoidtestGet005()throwsInterruptedException{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
     */@TestpublicvoidtestJsonBody001()throwsInterruptedException{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
     */@TestpublicvoidtestJsonBody002()throwsIOException,InterruptedException{Mono<String> mono;int num =RandomUtils.nextInt(1,4);switch(num){case1:// 方式1,javaBeanSearchReq req =newSearchReq();
                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;case2:// 方式2,HashMapMap<String,String> params =newHashMap<>();
                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;case3:// 方式3,json字符串Map<String,String> params2 =newHashMap<>();
                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
     */@TestpublicvoidtestUpload001()throwsInterruptedException{MultiValueMap<String,Object> params =newLinkedMultiValueMap<>();
        params.add("q1","java");
        params.add("q2","python");
        params.add("file",newClassPathResource("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
     */@TestpublicvoidtestUpload002()throwsInterruptedException{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",newClassPathResource("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.javapackagecom.fly.http;importjava.awt.Desktop;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.nio.charset.StandardCharsets;importjava.util.Collections;importjava.util.HashMap;importjava.util.Map;importjava.util.concurrent.TimeUnit;importjavax.imageio.ImageIO;importorg.apache.commons.lang3.RandomUtils;importorg.junit.BeforeClass;importorg.junit.Test;importorg.springframework.core.io.ClassPathResource;importorg.springframework.core.io.Resource;importorg.springframework.http.MediaType;importorg.springframework.util.FileCopyUtils;importorg.springframework.util.LinkedMultiValueMap;importorg.springframework.util.MultiValueMap;importorg.springframework.web.reactive.function.BodyInserters;importorg.springframework.web.reactive.function.client.ClientResponse;importorg.springframework.web.reactive.function.client.WebClient;importorg.springframework.web.util.UriComponentsBuilder;importcom.fly.http.bean.ImageShowDialog;importcom.fly.http.bean.JsonBeanUtils;importcom.fly.http.bean.SearchReq;importlombok.extern.slf4j.Slf4j;importreactor.core.publisher.Mono;/**
 * http请求WebClient同步调用实现
 */@Slf4jpublicclassWebClientSyncTest{privateWebClient webClient =WebClient.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build();privatevoidopenImage(Resource resource){try{newImageShowDialog(ImageIO.read(resource.getInputStream()));}catch(IOException e){
            log.error(e.getMessage(), e);}}@BeforeClasspublicstaticvoidinit(){newFile("download").mkdirs();}/**
     * WebClient同步调用
     * 
     * @throws IOException
     */@TestpublicvoidtestDownFile()throwsIOException{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(),newFileOutputStream("download/urls.txt"));}/**
     * WebClient同步调用
     * 
     * @throws IOException
     * @throws InterruptedException
     */@TestpublicvoidtestDownImg001()throwsIOException,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
     */@TestpublicvoidtestDownImg002()throwsIOException{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 =newFile(String.format("download/img_%s.jpg",System.currentTimeMillis()));FileCopyUtils.copy(resource.getInputStream(),newFileOutputStream(dest));if(Desktop.isDesktopSupported()){Desktop.getDesktop().open(dest.getParentFile());}}/**
     * WebClient同步调用
     */@TestpublicvoidtestExchange001(){// getMultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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同步调用
     */@TestpublicvoidtestExchange002(){// getMono<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 postMultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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同步调用
     */@TestpublicvoidtestFormDataPost(){MultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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同步调用
     */@TestpublicvoidtestGet001(){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同步调用
     */@TestpublicvoidtestGet002(){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同步调用
     * 
     */@TestpublicvoidtestGet003(){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
     * 
     */@TestpublicvoidtestGet004(){MultiValueMap<String,String> params =newLinkedMultiValueMap<>();
        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同步调用
     * 
     */@TestpublicvoidtestGet005(){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同步调用
     * 
     */@TestpublicvoidtestJsonBody001(){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
     */@TestpublicvoidtestJsonBody002()throwsIOException{Mono<String> mono;int num =RandomUtils.nextInt(1,4);switch(num){case1:// 方式1,javaBeanSearchReq req =newSearchReq();
                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;case2:// 方式2,HashMapMap<String,String> params =newHashMap<>();
                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;case3:// 方式3,json字符串Map<String,String> params2 =newHashMap<>();
                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同步调用
     * 
     */@TestpublicvoidtestUpload001(){MultiValueMap<String,Object> params =newLinkedMultiValueMap<>();
        params.add("q1","java");
        params.add("q2","python");
        params.add("file",newClassPathResource("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同步调用
     * 
     */@TestpublicvoidtestUpload002(){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",newClassPathResource("123.jpg"))).retrieve()// 获取响应体.bodyToMono(String.class);// 响应数据类型转换
        log.info("----- reponse: {}", mono.block());}}

**

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

**

-over-


本文转载自: https://blog.csdn.net/qq_16127313/article/details/136885093
版权归原作者 爱码少年 00fly.online 所有, 如有侵权,请联系我们删除。

“WebClient 同步、异步调用实现对比”的评论:

还没有评论