Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
woshizhushiqiu
spring-framework
提交
d25ae4b0
S
spring-framework
项目概览
woshizhushiqiu
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d25ae4b0
编写于
4月 21, 2021
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add advice on using exchange from an ExchangeFilterFunction
Closes gh-26819
上级
5740eaf3
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
65 addition
and
4 deletion
+65
-4
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunction.java
.../web/reactive/function/client/ExchangeFilterFunction.java
+8
-1
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFunction.java
...mework/web/reactive/function/client/ExchangeFunction.java
+8
-0
src/docs/asciidoc/web/webflux-webclient.adoc
src/docs/asciidoc/web/webflux-webclient.adoc
+49
-3
未找到文件。
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunction.java
浏览文件 @
d25ae4b0
/*
* Copyright 2002-20
19
the original author or authors.
* Copyright 2002-20
21
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -39,6 +39,13 @@ public interface ExchangeFilterFunction {
* in the chain, to be invoked via
* {@linkplain ExchangeFunction#exchange(ClientRequest) invoked} in order to
* proceed with the exchange, or not invoked to shortcut the chain.
*
* <p><strong>Note:</strong> When a filter handles the response after the
* call to {@link ExchangeFunction#exchange}, extra care must be taken to
* always consume its content or otherwise propagate it downstream for
* further handling, for example by the {@link WebClient}. Please, see the
* reference documentation for more details on this.
*
* @param request the current request
* @param next the next exchange function in the chain
* @return the filtered response
...
...
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFunction.java
浏览文件 @
d25ae4b0
...
...
@@ -43,6 +43,14 @@ public interface ExchangeFunction {
/**
* Exchange the given request for a {@link ClientResponse} promise.
*
* <p><strong>Note:</strong> When a calling this method from an
* {@link ExchangeFilterFunction} that handles the response in some way,
* extra care must be taken to always consume its content or otherwise
* propagate it downstream for further handling, for example by the
* {@link WebClient}. Please, see the reference documentation for more
* details on this.
*
* @param request the request to exchange
* @return the delayed response
*/
...
...
src/docs/asciidoc/web/webflux-webclient.adoc
浏览文件 @
d25ae4b0
...
...
@@ -889,9 +889,8 @@ a filter for basic authentication through a static factory method:
.build()
----
You can create a new `WebClient` instance by using another as a starting point. This allows
insert or removing filters without affecting the original `WebClient`. Below is an example
that inserts a basic authentication filter at index 0:
Filters can be added or removed by mutating an existing `WebClient` instance, resulting
in a new `WebClient` instance that does not affect the original one. For example:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
...
...
@@ -912,6 +911,53 @@ that inserts a basic authentication filter at index 0:
.build()
----
`WebClient` is a thin facade around the chain of filters followed by an
`ExchangeFunction`. It provides a workflow to make requests, to encode to and from higher
level objects, and it helps to ensure that response content is always consumed.
When filters handle the response in some way, extra care must be taken to always consume
its content or to otherwise propagate it downstream to the `WebClient` which will ensure
the same. Below is a filter that handles the `UNAUTHORIZED` status code but ensures that
any response content, whether expected or not, is released:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
public ExchangeFilterFunction renewTokenFilter() {
return (request, next) -> next.exchange(request).flatMap(response -> {
if (response.statusCode().value() == HttpStatus.UNAUTHORIZED.value()) {
return response.releaseBody()
.then(renewToken())
.flatMap(token -> {
ClientRequest newRequest = ClientRequest.from(request).build();
return next.exchange(newRequest);
});
} else {
return Mono.just(response);
}
});
}
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
fun renewTokenFilter(): ExchangeFilterFunction? {
return ExchangeFilterFunction { request: ClientRequest?, next: ExchangeFunction ->
next.exchange(request!!).flatMap { response: ClientResponse ->
if (response.statusCode().value() == HttpStatus.UNAUTHORIZED.value()) {
return@flatMap response.releaseBody()
.then(renewToken())
.flatMap { token: String? ->
val newRequest = ClientRequest.from(request).build()
next.exchange(newRequest)
}
} else {
return@flatMap Mono.just(response)
}
}
}
}
----
[[webflux-client-attributes]]
== Attributes
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录