属性

您可以向请求添加属性。如果您希望通过过滤器链传递信息并影响给定请求的过滤器行为,这将非常方便。例如:

  • Java

  • Kotlin

WebClient client = WebClient.builder()
		.filter((request, next) -> {
			Optional<Object> usr = request.attribute("myAttribute");
			// ...
		})
		.build();

client.get().uri("https://example.org/")
		.attribute("myAttribute", "...")
		.retrieve()
		.bodyToMono(Void.class);

	}
val client = WebClient.builder()
		.filter { request, _ ->
			val usr = request.attributes()["myAttribute"];
			// ...
		}
		.build()

	client.get().uri("https://example.org/")
			.attribute("myAttribute", "...")
			.retrieve()
			.awaitBody<Unit>()

请注意,您可以在WebClient.Builder级别全局配置defaultRequest回调,这样可以将属性插入所有请求中,例如在Spring MVC应用程序中,可以基于ThreadLocal数据填充请求属性。