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
This commit is contained in:
Arjen Poutsma 2022-07-28 14:09:11 +02:00
parent 97ea8a6789
commit b8b54ee524
2 changed files with 15 additions and 3 deletions

View File

@ -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<String, List<String>> entry : headers.entrySet()) {
for (String value : entry.getValue()) {
result.add(entry.getKey(), value);
}
}
return result;
}

View File

@ -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<String, List<String>> entry : headers.entrySet()) {
for (String value : entry.getValue()) {
result.add(entry.getKey(), value);
}
}
return result;
}
}