From b8b54ee5242af7158b811b6049c89af5218dc2c5 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 28 Jul 2022 14:09:11 +0200 Subject: [PATCH] Ensure WebClient exceptions are serializable Not all HttpHeaders implementations are serializable. This commit ensures that WebClientRequestException and WebClientResponseException are serializable, by copying any non-serializable HttpHeaders into a new, serializable, instance. Closes gh-28321 --- .../function/client/WebClientRequestException.java | 8 +++++++- .../function/client/WebClientResponseException.java | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientRequestException.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientRequestException.java index 8e8c5c3e334..6b4d9c39a26 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientRequestException.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientRequestException.java @@ -17,6 +17,8 @@ package org.springframework.web.reactive.function.client; import java.net.URI; +import java.util.List; +import java.util.Map; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -56,7 +58,11 @@ public class WebClientRequestException extends WebClientException { */ private static HttpHeaders copy(HttpHeaders headers) { HttpHeaders result = new HttpHeaders(); - result.putAll(headers); + for (Map.Entry> entry : headers.entrySet()) { + for (String value : entry.getValue()) { + result.add(entry.getKey(), value); + } + } return result; } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java index 62b5a7b3afb..c449cd35d45 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java @@ -18,6 +18,8 @@ package org.springframework.web.reactive.function.client; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpRequest; @@ -47,7 +49,7 @@ public class WebClientResponseException extends WebClientException { private final Charset responseCharset; @Nullable - private final HttpRequest request; + private transient final HttpRequest request; /** @@ -112,7 +114,11 @@ public class WebClientResponseException extends WebClientException { } else { HttpHeaders result = new HttpHeaders(); - result.putAll(headers); + for (Map.Entry> entry : headers.entrySet()) { + for (String value : entry.getValue()) { + result.add(entry.getKey(), value); + } + } return result; } }