一灰灰blog 一灰灰blog
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)

一灰灰blog

资深搬运工
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)
  • 基础系列

  • DB系列

  • 搜索系列

  • MQ系列

  • WEB系列

    • Request

    • Response

    • RestTemplate

    • WebClient

      • 【WEB系列】WebClient之基础使用姿势
      • 【WEB系列】WebClient之文件上传
      • 【WEB系列】WebClient之请求头设置
      • 【WEB系列】WebClient之Basic Auth授权
      • 【WEB系列】WebClient之超时设置
      • 【WEB系列】WebClient之retrieve与exchange的使用区别介绍
      • 【WEB系列】WebClient之非200状态码信息捕获
      • 【WEB系列】WebClient之策略设置
        • I. 项目环境
          • 1. 依赖
          • 2. 测试接口
        • II. 使用说明
          • 1. MaxInMemorySize设置
          • 2. 编解码设置
        • II. 其他
          • 0. 项目
          • 1. 一灰灰Blog
      • 【WEB系列】WebClient之同步与异步
    • WebFlux

    • WebSocket

    • Web三剑客

    • 实例

    • 其他

  • 中间件

  • 运维

  • SpringSecurity

  • SpringCloud

  • Spring系列
  • WEB系列
  • WebClient
一灰灰
2020-08-11

【WEB系列】WebClient之策略设置

在前面介绍WebClient的常见参数中,有一个exchangeStrategies参数设置,通过它我们可以设置传输数据的内存占用大小限制,避免内存问题;也可以通过它设置数据的编解码

# I. 项目环境

本项目借助SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA进行开发

# 1. 依赖

使用WebClient,最主要的引入依赖如下(省略掉了SpringBoot的相关依赖,如对于如何创建SpringBoot项目不太清楚的小伙伴,可以关注一下我之前的博文)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
1
2
3
4

# 2. 测试接口

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Body implements Serializable {
    private static final long serialVersionUID = 1210673970258821332L;
    String name;
    Integer age;
}

@GetMapping(path = "get")
public Mono<String> get(String name, Integer age) {
    return Mono.just("req: " + name + " age: " + age);
}

@PostMapping(path = "body2")
public Mono<Body> postBody2(@RequestBody  Body body) {
    return Mono.just(body);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# II. 使用说明

# 1. MaxInMemorySize设置

默认情况下,内存中接收数据的buffering data size为256KB,可以根据实际的需要进行改大or调小

// 默认允许的内存空间大小为256KB,可以通过下面的方式进行修改
webClient = WebClient.builder().exchangeStrategies(
        ExchangeStrategies.builder().codecs(codec -> codec.defaultCodecs().maxInMemorySize(10)).build())
        .baseUrl("http://127.0.0.1:8080").build();

String argument = "这也是一个很长很长的文本,用于测试超出上限!";
Mono<String> ans = webClient.get().uri("/get?name={1}", argument).retrieve().bodyToMono(String.class)
        // 异常处理
        .doOnError(WebClientResponseException.class, err -> {
            System.out.println(err.getRawStatusCode() + "," + err.getResponseBodyAsString());
            throw new RuntimeException(err.getMessage());
        }).onErrorReturn("fallback");
ans.subscribe(s -> System.out.println("exchange strategy: " + ans));
1
2
3
4
5
6
7
8
9
10
11
12
13

# 2. 编解码设置

比如最常见的json编解码

WebClient webClient = WebClient.builder().exchangeStrategies(ExchangeStrategies.builder().codecs(codec -> {
    codec.customCodecs().decoder(new Jackson2JsonDecoder());
    codec.customCodecs().encoder(new Jackson2JsonEncoder());
}).build()).baseUrl("http://127.0.0.1:8080").build();
Body body = new Body("一灰灰😝", 18);
Mono<Body> ans =
        webClient.post().uri("/body2").contentType(MediaType.APPLICATION_JSON).bodyValue(body).retrieve()
                .bodyToMono(Body.class);
ans.subscribe(s -> System.out.println("retreive res: " + s));
1
2
3
4
5
6
7
8
9

上面两个测试之后,返回结果如下

IMAGE

# II. 其他

# 0. 项目

系列博文

  • 【WEB系列】WebClient之非200状态码信息捕获 (opens new window)
  • 【WEB系列】WebClient之retrieve与exchange的使用区别介绍 (opens new window)
  • 【WEB系列】WebClient之超时设置 (opens new window)
  • 【WEB系列】WebClient之Basic Auth授权 (opens new window)
  • 【WEB系列】WebClient之请求头设置 (opens new window)
  • 【WEB系列】WebClient之文件上传 (opens new window)
  • 【WEB系列】WebClient之基础使用姿势 (opens new window)

源码

  • 工程:https://github.com/liuyueyi/spring-boot-demo (opens new window)
  • 源码:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/222-web-client (opens new window)

# 1. 一灰灰Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰Blog个人博客 https://blog.hhui.top (opens new window)
  • 一灰灰Blog-Spring专题博客 http://spring.hhui.top (opens new window)

一灰灰blog

编辑 (opens new window)
#WebClient
上次更新: 2021/10/15, 19:56:22
【WEB系列】WebClient之非200状态码信息捕获
【WEB系列】WebClient之同步与异步

← 【WEB系列】WebClient之非200状态码信息捕获 【WEB系列】WebClient之同步与异步→

最近更新
01
【基础系列】基于maven多环境配置
04-25
02
【WEB系列】内嵌Tomcat配置Accesslog日志文件生成位置源码探索
04-24
03
【搜索系列】ES查询常用实例演示
04-18
更多文章>
Theme by Vdoing | Copyright © 2017-2022 一灰灰Blog
MIT License | 鄂ICP备18017282号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×