diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java index 6d0331b9bd4..927fcdf205d 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -17,7 +17,6 @@ package org.springframework.web.reactive.function.client; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; import java.util.Map; @@ -207,9 +206,7 @@ class DefaultClientResponse implements ClientResponse { .onErrorReturn(IllegalStateException.class::isInstance, EMPTY) .map(bodyBytes -> { HttpRequest request = this.requestSupplier.get(); - Charset charset = headers().contentType() - .map(MimeType::getCharset) - .orElse(StandardCharsets.ISO_8859_1); + Charset charset = headers().contentType().map(MimeType::getCharset).orElse(null); int statusCode = rawStatusCode(); HttpStatus httpStatus = HttpStatus.resolve(statusCode); if (httpStatus != null) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/UnknownHttpStatusCodeException.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/UnknownHttpStatusCodeException.java index 50c53a52f68..07550a11dbd 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/UnknownHttpStatusCodeException.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/UnknownHttpStatusCodeException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 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. @@ -50,7 +50,7 @@ public class UnknownHttpStatusCodeException extends WebClientResponseException { * @since 5.1.4 */ public UnknownHttpStatusCodeException( - int statusCode, HttpHeaders headers, byte[] responseBody, Charset responseCharset, + int statusCode, HttpHeaders headers, byte[] responseBody, @Nullable Charset responseCharset, @Nullable HttpRequest request) { super("Unknown status code [" + statusCode + "]", statusCode, "", 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 82d246c3f00..ab211917b5f 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-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -43,6 +43,7 @@ public class WebClientResponseException extends WebClientException { private final HttpHeaders headers; + @Nullable private final Charset responseCharset; @Nullable @@ -97,7 +98,7 @@ public class WebClientResponseException extends WebClientException { this.statusText = statusText; this.headers = (headers != null ? headers : HttpHeaders.EMPTY); this.responseBody = (responseBody != null ? responseBody : new byte[0]); - this.responseCharset = (charset != null ? charset : StandardCharsets.ISO_8859_1); + this.responseCharset = charset; this.request = request; } @@ -139,10 +140,26 @@ public class WebClientResponseException extends WebClientException { } /** - * Return the response body as a string. + * Return the response content as a String using the charset of media type + * for the response, if available, or otherwise falling back on + * {@literal ISO-8859-1}. Use {@link #getResponseBodyAsString(Charset)} if + * you want to fall back on a different, default charset. */ public String getResponseBodyAsString() { - return new String(this.responseBody, this.responseCharset); + return getResponseBodyAsString(StandardCharsets.ISO_8859_1); + } + + /** + * Variant of {@link #getResponseBodyAsString()} that allows specifying the + * charset to fall back on, if a charset is not available from the media + * type for the response. + * @param defaultCharset the charset to use if the {@literal Content-Type} + * of the response does not specify one. + * @since 5.3.7 + */ + public String getResponseBodyAsString(Charset defaultCharset) { + return new String(this.responseBody, + (this.responseCharset != null ? this.responseCharset : defaultCharset)); } /**