From 4932196b8755ac203166d139a061c29c7c0c238b Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 23 Oct 2019 16:25:54 +0100 Subject: [PATCH 1/2] RestClientResponseException defaults to UTF-8 Closes gh-23764 --- .../web/client/RestClientResponseException.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java b/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java index 36006cc483..60db2528f1 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -33,7 +33,7 @@ public class RestClientResponseException extends RestClientException { private static final long serialVersionUID = -8803556342728481792L; - private static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1; + private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; private final int rawStatusCode; From 2733511ea2fbd3f905a7a89ab809c1f6e46276fd Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 23 Oct 2019 16:49:32 +0100 Subject: [PATCH 2/2] Charset argument in RestClientResponseException See gh-23764 --- .../web/client/RestClientResponseException.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java b/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java index 60db2528f1..2a927f3769 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestClientResponseException.java @@ -99,11 +99,22 @@ public class RestClientResponseException extends RestClientException { } /** - * Return the response body as a string. + * Return the response body converted to String. The charset used is that + * of the response "Content-Type" or otherwise {@code "UTF-8"}. */ public String getResponseBodyAsString() { + return getResponseBodyAsString(DEFAULT_CHARSET); + } + + /** + * Return the response body converted to String. The charset used is that + * of the response "Content-Type" or otherwise the one given. + * @param fallbackCharset the charset to use on if the response doesn't specify. + * @since 5.1.11 + */ + public String getResponseBodyAsString(Charset fallbackCharset) { if (this.responseCharset == null) { - return new String(this.responseBody, DEFAULT_CHARSET); + return new String(this.responseBody, fallbackCharset); } try { return new String(this.responseBody, this.responseCharset);