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

news2024/11/17 3:50:52

文章目录

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

一、概述

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、异步代码

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); // 重要,等待异步调用完成
    }
}

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());
    }
}

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的替代品,有更好的响应式能力,支持异步调用<br>
 * 
 * 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());
    }
}


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

-over-

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1542084.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

初探Flink集群【持续更新】

周末下雨&#xff0c;倒杯茶&#xff0c;在家练习Flink相关。 开发工具&#xff1a;IntelliJ Idea 第一步、创建项目 打开Idea&#xff0c;新建Maven项目&#xff0c;包和项目命名 在pom.xml 文件中添加依赖 <properties><flink.version>1.13.0</flink.vers…

python(django(自动化))之流程接口展示功能前端开发

1、创建模板代码如下&#xff1a; <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><title>测试平台</title> </head> <body role"document"> <nav class "navbar n…

轻量级 C++ UI 库:快速、可移植、自包含 | 开源日报 No.168

ocornut/imgui Stars: 53.4k License: MIT imgui 是 C 的无臃肿图形用户界面&#xff0c;具有最小的依赖关系。 该项目的主要功能、关键特性、核心优势包括&#xff1a; 为 C 提供了一个轻量级的图形用户界面库输出优化的顶点缓冲区&#xff0c;可在 3D 渲染应用程序中随时呈…

2024/03/24----Pycharm社区版本下载以及创建一个新项目

1.本科安装过这个软件&#xff0c;因为系统重置了&#xff0c;所以重头再来一遍。 2.链接 社区版本Pycharm 3.我下载了2023.1.15的版本&#xff0c;越前面一些的版本可能会比较稳定 4.安装步骤 &#xff08;1&#xff09;先把文件安装目录改成其他盘(文件名最好不要有数字和符号…

2025汤家凤考研数学视频,基础网课百度网盘课程+PDF讲义资料

2025汤家凤大神及数学全程 docs.qq.com/doc/DTmtOa0Fzc0V3WElI 复制粘贴到浏览器&#xff0c;可以见所有的Ke 第一轮 夯实基础 1.阅读大纲考查要求&#xff0c;明确每章的学习目标&#xff1b; 2.按节学习数学理论基础知识&#xff0c;吃透书中例题&#xff1b; 3.学习每章…

Django 三板斧、静态文件、request方法

【一】三板斧 【1】HttpResponse &#xff08;1&#xff09;介绍 HttpResponse是Django中的一个类&#xff0c;用于构建HTTP响应对象。它允许创建并返回包含特定内容的HTTP响应。 &#xff08;2&#xff09;使用 导入HttpResponse类 from django.http import HttpResponse创…

Python学习从0到1 day18 Python可视化基础综合案例 1.折线图

我默记这段路的酸楚&#xff0c;等来年春暖花开之时再赏心阅读 —— 24.3.24 python基础综合案例 数据可视化 — 折线图可视化 一、折线图案例 1.json数据格式 2.pyecharts模块介绍 3.pyecharts快速入门 4.数据处理 5.创建折线图 1.json数据格式 1.什么是json 2.掌握如何使用js…

【云开发笔记No.9】Kanban与敏捷开发

Kanban看板起源于丰田。 看板&#xff08;Kanban&#xff09;一词来自日文&#xff0c;本义是可视化卡片。如下图所示&#xff0c;看板工具的实质是&#xff1a;后道工序在需要时&#xff0c;通过看板向前道工序发出信号——请给我需要数量的输入&#xff0c;前道工序只有得到看…

阿里云4核16G服务器优惠价格26元1个月、149元半年

阿里云4核16G服务器优惠价格26.52元1个月、79.56元3个月、149.00元半年。2024年腾讯云服务器优惠价格表&#xff0c;一张表整理阿里云服务器最新报价&#xff0c;阿里云服务器网整理云服务器ECS和轻量应用服务器详细CPU内存、公网带宽和系统盘详细配置报价单&#xff0c;大家也…

[iOS]GCD(一)

[iOS]GCD(一) 文章目录 [iOS]GCD(一)GCD的概要GCD的APIDispatch Queuedispatch_queue_createMain Dispatch_set_target_queuedispatch_afterDispatch Groupdispatch_barrier_asyncdispatch_applydispatch_applydispatch_suspend/dispatch_resumeDispatch Semaphoredispatch_onc…

力扣:290. 单词规律

前言&#xff1a;剑指offer刷题系列 问题&#xff1a; 给定一种规律 pattern 和一个字符串 s &#xff0c;判断 s 是否遵循相同的规律。 这里的 遵循 指完全匹配&#xff0c;例如&#xff0c; pattern 里的每个字母和字符串 s 中的每个非空单词之间存在着双向连接的对应规律…

大数据Spark--入门

文章目录 Spark 概述Spark 是什么Spark and HadoopSpark and HadoopSpark 核心模块 Spark 简单上手创建Maven项目增加 Scala 插件增加依赖关系WordCount异常处理 Spark 概述 Spark 所需资料 链接&#xff1a;https://pan.baidu.com/s/12iaW68vriL6i-xI1kmr0_g?pwdm4zc 提取码…

跨时钟域学习记录(一)

亚稳态 亚稳态是电平介于高低电平之间的一种暂时状态。在同步系统中&#xff0c;当寄存器的信号无法满足建立时间和保持时间时&#xff0c;输出端的信号就可能出现亚稳态。在异步系统中&#xff0c;亚稳态并非一定在建立时间和保持时间无法满足时出现。   受噪声、温度、电压…

MongoDB高可用架构涉及常用功能整理

MongoDB高可用架构涉及常用功能整理 1. mongo架构和相关组件1.1. Master-Slave主从模式1.2. Replica Set 副本集模式1.3. Sharding 分片模式 2. Sharding 分片模式2.1. Hashed Sharding方式2.2. Range Sharding方式 3. 事务性4. 疑问和思考4.1. 怎么保证数据的高可靠&#xff1…

oracle 19c RAC补丁升级

1.停止集群件备份家目录 ----两节点分别操作 cd /u01/app/19.3.0/grid/bin/ crsctl stop crstar -zcvf /u01/app.tar.gz /u01/app/u01/app/19.0.0/grid/bin/crsctl start crs2.两节点 GI、DB OPatch 替换&#xff08;都得执行&#xff09; ----# 表示 root 用户&#xff0c;$…

UDP建立聊天群

参考网上代码 接收端 #include<myhead.h> #define PRINT_ERR(msg) \ do \ { \ printf("%s,…

docker 本地机 互通文件

查询容器name 查询容器Id 进行传输

Windows11 使用 VirtualBox 安装创建 Ubuntu虚拟机

〇、背景 开发者大比例习惯都是Windows下编辑代码&#xff0c;比如使用Windows的Visual Studio Code进行代码的开发。但不管是AOSP还是鸿蒙开发&#xff0c;目前都不支持windows本地环境编译的&#xff0c;建议使用Ubuntu操作系统环境对源码进行编译。 因此&#xff0c;没有U…

android emulator windows bat启动

android emulator windows bat启动 先上结果 // 模拟器路径 -netspeed full -avd 模拟器名称 C:\Users\name\AppData\Local\Android\Sdk\emulator\emulator.exe -netdelay none -netspeed full -avd Pixel_3a_API_34_extension_level_7_x86_64一般来说 windows 如果不做…

springcloud第4季 负载均衡的介绍3

一 loadbalance 1.1 负载均衡的介绍 使用注解loadbalance&#xff0c;是一个客户端的负载均衡器&#xff1b;通过之前已经从注册中心拉取缓存到本地的服务列表中&#xff0c;获取服务进行轮询负载请求服务列表中的数据。 轮询原理 1.2 loadbalance工作流程 loadBalance工作…