diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/WebClientExtensions.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/WebClientExtensions.kt index d48b0b020761..74cb81af9231 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/WebClientExtensions.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/WebClientExtensions.kt @@ -143,6 +143,17 @@ suspend inline fun WebClient.ResponseSpec.awaitBody() : T = else -> bodyToMono().awaitSingle() } +/** + * Coroutines variant of [WebClient.ResponseSpec.bodyToMono]. + * + * @author Valentin Shakhov + */ +suspend inline fun WebClient.ResponseSpec.awaitBodyOrNull() : T? = + when (T::class) { + Unit::class -> awaitBodilessEntity().let { Unit as T } + else -> bodyToMono().awaitSingleOrNull() + } + /** * Coroutines variant of [WebClient.ResponseSpec.toBodilessEntity]. */