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 573fe6559d8c..8e8c5c3e3342 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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. @@ -47,9 +47,20 @@ public WebClientRequestException(Throwable ex, HttpMethod method, URI uri, HttpH this.method = method; this.uri = uri; - this.headers = headers; + this.headers = copy(headers); } + /** + * Not all {@code HttpHeaders} implementations are serializable, so we + * make a copy to ensure that {@code WebClientResponseException} is. + */ + private static HttpHeaders copy(HttpHeaders headers) { + HttpHeaders result = new HttpHeaders(); + result.putAll(headers); + return result; + } + + /** * Return the HTTP request method. */ 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 a3d1bb94a1c6..62b5a7b3afb9 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -96,12 +96,27 @@ public WebClientResponseException(String message, int statusCode, String statusT this.statusCode = statusCode; this.statusText = statusText; - this.headers = (headers != null ? headers : HttpHeaders.EMPTY); + this.headers = copy(headers); this.responseBody = (responseBody != null ? responseBody : new byte[0]); this.responseCharset = charset; this.request = request; } + /** + * Not all {@code HttpHeaders} implementations are serializable, so we + * make a copy to ensure that {@code WebClientResponseException} is. + */ + private static HttpHeaders copy(@Nullable HttpHeaders headers) { + if (headers == null) { + return HttpHeaders.EMPTY; + } + else { + HttpHeaders result = new HttpHeaders(); + result.putAll(headers); + return result; + } + } + /** * Return the HTTP status code value.