【WEB系列】WebClient之非200状态码信息捕获
前面介绍的WebClient的使用姿势都是基于正常的200状态码返回,当然在实际的使用中,我们并不是总能获取到200的状态码的,在RestTemplate的学习中,我们知道如果不特殊处理,那么是无法正确获取非200状态码的ResponseBody的,会直接抛出异常,那么在WebClient中又应该怎样应对非200状态码返回的场景呢?
# 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
3
4
# 2. REST
一个简单的403返回
@GetMapping(path = "403")
public Mono<String> _403(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
response.setStatusCode(HttpStatus.FORBIDDEN);
return Mono.just("403 response body!");
}
1
2
3
4
5
6
2
3
4
5
6
# II. 实例说明
# 1. retrieve方式
上一篇介绍retrieve与exchange区别的博文中,我们知道retrieve更适用于只希望获取ResponseBody的场景;使用retrieve时,如需要捕获其他状态码的返回,可以如下操作
Mono<String> ans = webClient.get().uri("403").retrieve().onStatus(HttpStatus::is4xxClientError, response -> {
System.out.println("inner retrieve 403 res: " + response.headers().asHttpHeaders() + "|" + response.statusCode());
response.bodyToMono(String.class).subscribe(s -> System.out.println("inner res body: " + s));
return Mono.just(new RuntimeException("403 not found!"));
}).bodyToMono(String.class);
ans.subscribe(s -> System.out.println("retrieve 403 ans: " + s));
1
2
3
4
5
6
2
3
4
5
6
请注意上面的 onStatus
, 上面演示的是4xx的捕获,返回如下
inner retrieve 403 res: [Content-Type:"text/plain;charset=UTF-8", Content-Length:"18"]|403 FORBIDDEN
1
# 2. exchange方式
exchange本身就可以获取完整的返回信息,所以异常的case需要我们自己在内部进行处理
webClient.get().uri("403").exchange().subscribe(s -> {
HttpStatus statusCode = s.statusCode();
ClientResponse.Headers headers = s.headers();
MultiValueMap<String, ResponseCookie> cookies = s.cookies();
s.bodyToMono(String.class).subscribe(body -> {
System.out.println(
"error response detail: \nheader: " + headers.asHttpHeaders() + "\ncode: " + statusCode +
"\ncookies: " + cookies + "\nbody:" + body);
});
});
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
返回结果如下
exchange error response detail:
header: [Content-Type:"text/plain;charset=UTF-8", Content-Length:"18"]
code: 403 FORBIDDEN
cookies: {}
body:403 response body!
1
2
3
4
5
2
3
4
5
# II. 其他
# 0. 项目
系列博文
- 【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)
编辑 (opens new window)
上次更新: 2021/10/15, 19:56:22
- 01
- 【基础系列】基于maven多环境配置04-25
- 03
- 【搜索系列】ES查询常用实例演示04-18