【SpringBoot 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.bootgroupId>
<artifactId>spring-boot-starter-webfluxartifactId>
dependency>
2. REST
一个简单的 403 返回
@GetMapping(path = "403")
public Mono _403(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
response.setStatusCode(HttpStatus.FORBIDDEN);
return Mono.just("403 response body!");
}
II. 实例说明
1. retrieve 方式
上一篇介绍 retrieve 与 exchange 区别的博文中,我们知道 retrieve 更适用于只希望获取 ResponseBody 的场景;使用 retrieve 时,如需要捕获其他状态码的返回,可以如下操作
Mono 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));
请注意上面的 onStatus
, 上面演示的是 4xx 的捕获,返回如下
inner retrieve 403 res: [Content-Type:"text/plain;charset=UTF-8", Content-Length:"18"]|403 FORBIDDEN
2. exchange 方式
exchange 本身就可以获取完整的返回信息,所以异常的 case 需要我们自己在内部进行处理
webClient.get().uri("403").exchange().subscribe(s -> {
HttpStatus statusCode = s.statusCode();
ClientResponse.Headers headers = s.headers();
MultiValueMap cookies = s.cookies();
s.bodyToMono(String.class).subscribe(body -> {
System.out.println("error response detail: \nheader: " + headers.asHttpHeaders() + "\ncode: " + statusCode +"\ncookies: " + cookies + "\nbody:" + body);
});
});
返回结果如下
exchange error response detail:
header: [Content-Type:"text/plain;charset=UTF-8", Content-Length:"18"]
code: 403 FORBIDDEN
cookies: {}
body:403 response body!
II. 其他
0. 项目
WebClient系列博文
- 【WEB 系列】WebClient 之 retrieve 与 exchange 的使用区别介绍
- 【WEB 系列】WebClient 之超时设置
- 【WEB 系列】WebClient 之 Basic Auth 授权
- 【WEB 系列】WebClient 之请求头设置
- 【WEB 系列】WebClient 之文件上传
- 【WEB 系列】WebClient 之基础使用姿势
- 工程:https://github.com/liuyueyi/spring-boot-demo
- 源码:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/222-web-client
1. 一灰灰 Blog
尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现 bug 或者有更好的建议,欢迎批评指正,不吝感激
下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
- 一灰灰 Blog 个人博客 https://blog.hhui.top
- 一灰灰 Blog-Spring 专题博客 http://spring.hhui.top
【SpringBoot WEB 系列】AsyncRestTemplate 之异步非阻塞网络请求介绍篇
【SpringBoot WEB系列】 RestTemplate之文件上传
【SpringBoot WEB 系列】RestTemplate 之非 200 状态码信息捕获
【SpringBoot WEB 系列】RestTemplate 之 Basic Auth 授权
【SpringBoot WEB 系列】RestTemplate 之代理访问
【SpringBoot WEB 系列】RestTemplate 之超时设置
【SpringBoot WEB 系列】RestTemplate 之中文乱码问题 fix
【SpringBoot WEB 系列】RestTemplate 之自定义请求头
【SpringBoot WEB 系列】RestTemplate 基础用法小结
【SpringBoot WEB 系列】RestTemplate 4xx/5xx 异常信息捕获
【SpringBoot WEB 系列】RestTempalte urlencode参数解析异常全程分析